简体   繁体   English

带有滚动视图和键盘的iOS AutoLayout

[英]iOS AutoLayout with scrollview and keyboard

I have one view on my storyboard to show user log in form, so it looks like this: main view->scroll view->content view->two text fields and log in button at the top and one register button at the bottom of the view. 我在故事板上有一个视图来显示用户登录表单,所以它看起来像这样:主视图 - >滚动视图 - >内容视图 - >顶部的两个文本字段和登录按钮以及底部的一个注册按钮风景。 I use autolayout and bottom button has bottom space constraint. 我使用自动布局,底部按钮有底部空间约束。 When I tap on the text field and keyboard appears I would like to scroll view to change size to visible rect, but content size should remain to scroll down to the register button, but the button move up when the size of the scroll view change. 当我点击文本字段并出现键盘时,我想滚动视图以将大小更改为可见的rect,但内容大小应保持向下滚动到注册按钮,但当滚动视图的大小更改时按钮会向上移动。 How could I do what I want? 我怎么能做我想要的?

I use this code when keyboard appears: 键盘出现时我使用此代码:

- (void)keyboardWillShow:(NSNotification *)aNotification
{
    NSDictionary *info = [aNotification userInfo];
    NSValue *kbFrame = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    NSTimeInterval animationDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    CGRect keyboardFrame = [kbFrame CGRectValue];

    CGSize s = self.scrollView.contentSize;
    CGFloat height = keyboardFrame.size.height;
    self.scrollViewBottomLayoutConstraint.constant = height;

    [UIView animateWithDuration:animationDuration animations:^{
        [self.view layoutIfNeeded];
        [self.scrollView setContentSize:s];
    }];
}

Try leaving the content size alone, and instead adjust the scroll view's contentInset property. 尝试单独保留内容大小,而是调整滚动视图的contentInset属性。 Then you don't have to mess with constraints. 然后你不必乱用约束。

- (void)keyboardUp:(NSNotification *)notification
{
    NSDictionary *info = [notification userInfo];
    CGRect keyboardRect = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    keyboardRect = [self.view convertRect:keyboardRect fromView:nil];

    UIEdgeInsets contentInset = self.scrollView.contentInset;
    contentInset.bottom = keyboardRect.size.height;
    self.scrollView.contentInset = contentInset;
}

The best way I have found is to override NSLayoutConstraint like so: 我发现的最好方法是覆盖NSLayoutConstraint,如下所示:

@implementation NHKeyboardLayoutConstraint

- (void) dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void) awakeFromNib {
    [super awakeFromNib];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillChangeFrame:)
                                                 name:UIKeyboardWillChangeFrameNotification
                                               object:nil];
}

- (void)keyboardWillChangeFrame:(NSNotification *)notification {

    CGRect endKBRect = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];

    CGFloat animationDuration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
    CGRect frame = [UIApplication sharedApplication].keyWindow.bounds;

    self.constant = frame.size.height - endKBRect.origin.y;


    [UIView animateWithDuration:animationDuration animations:^{
        [[UIApplication sharedApplication].keyWindow layoutIfNeeded];
    }];
}


@end

then in your xib change the class type of the offending constraint to this class. 然后在你的xib中将违规约束的类类型更改为此类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM