【问题标题】:iOS: Change contentsize of a UIScrollview using auto layoutiOS:使用自动布局更改 UIScrollview 的内容大小
【发布时间】:2012-09-27 17:30:50
【问题描述】:

我正在UIScrollView 中实现一个表单。当键盘打开时,我试图在滚动视图内容的底部添加一些空间,以便用户可以看到所有字段。我将表单视图放在UISCrollView 中,使用以下代码添加所有需要的约束:

 [_infoView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_infoView(1730)]" options:0 metrics:nil views:views]];

[_scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_infoView]|" options:0 metrics:nil views:views]];

[_scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_infoView]|" options:0 metrics:nil views:views]];

[_scrollView addConstraint:[NSLayoutConstraint constraintWithItem:_infoView
                                                        attribute:NSLayoutAttributeCenterX
                                                        relatedBy:NSLayoutRelationEqual
                                                           toItem:_scrollView
                                                        attribute:NSLayoutAttributeCenterX
                                                       multiplier:1
                                                         constant:0]];

如您所见,我在第一行指定了表单的高度,scrollview 会自动调整其内容大小。现在我想增加表单的高度,所以我尝试用更大的高度重置约束,但它不起作用。然后我尝试使用[_scrollView setContentSize:] 方法,但这也行不通。谁能帮帮我?

【问题讨论】:

  • 你有没有想过这个问题?我很难弄清楚这一点。

标签: ios uiscrollview ios6 autolayout


【解决方案1】:

如果我理解这个问题,我建议调整UIScrollView 上的contentInset 属性,而不是调用layoutSubviews。请注意,文档说:

使用此属性可添加到内容周围的滚动区域。大小的单位是点。默认值为UIEdgeInsetsZero

您仍然需要收听键盘隐藏/显示通知,以便知道何时调整滚动视图的高度:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

然后在keyboardWillShow 你可以这样做:

UIEdgeInsets insets = UIEdgeInsetsMake(0, 0, 100, 0);
scrollView.contentInset = insets;

100 是您要调整 scrollView 的高度。我使用UITableView 执行此操作,其中我的表单元素为UITableViewCells,并且效果很好。

【讨论】:

    【解决方案2】:

    我不确定你在哪里添加上面的代码,但下面应该可以解决你的问题

    在您的 init 函数中,添加以下内容:

    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
            [center addObserver:self selector:@selector(noticeShowKeyboard:) name:UIKeyboardDidShowNotification object:nil];
            [center addObserver:self selector:@selector(noticeHideKeyboard:) name:UIKeyboardWillHideNotification object:nil];
    

    将以下内容添加到您的 .h

    CGSize keyboardSize;
    int keyboardHidden;      // 0 @ initialization, 1 if shown, 2 if hidden
    

    将以下内容添加到您的 .m

    -(void) noticeShowKeyboard:(NSNotification *)inNotification {
        keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
        keyboardHidden = 1;
        [self layoutSubviews];        // Not sure if it is called automatically, so I called it
    
    
    }
    -(void) noticeHideKeyboard:(NSNotification *)inNotification {
        keyboardHidden = 2;
        [self layoutSubviews];       // Not sure if it is called automatically, so I called it
    
    }
    
    - (void) layoutSubviews
    {
        [super layoutSubviews];
    
        if(keyboardHidden == 1) {
            scrollview.frame = CGRectMake(scrollview.frame.origin.x, scrollview.frame.origin.y, scrollview.frame.size.width, scrollview.frame.size.height + keyboardSize.height);
        }
        else if(keyboardHidden == 2) {
            scrollview.frame = CGRectMake(scrollview.frame.origin.x, scrollview.frame.origin.y, scrollview.frame.size.width, scrollview.frame.size.height - keyboardSize.height);
        }
    }
    

    我覆盖了layoutsubviews,现在我认为它应该可以工作了。

    【讨论】:

    • 问题是,如果你的笔尖使用自动布局,矩形大小是由约束管理的,直接设置它们是行不通的
    • 检查编辑的答案,覆盖layoutsubviews 应该可以解决您的问题...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-08
    • 1970-01-01
    • 2013-07-25
    • 1970-01-01
    • 2011-07-24
    相关资源
    最近更新 更多