9

我在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:]方法,但这也行不通。任何人都可以帮助我吗?

4

2 回答 2

4

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

使用此属性可添加到内容周围的滚动区域。大小的单位是点。默认值为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。我使用 a 来执行此操作UITableView,其中我的表单元素为UITableViewCells,并且效果很好。

于 2013-04-14T19:27:20.180 回答
-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了,现在我认为它应该可以工作。

于 2012-11-05T11:36:46.737 回答