iOS的自动版式与滚动型和键盘 [英] iOS AutoLayout with scrollview and keyboard

查看:126
本文介绍了iOS的自动版式与滚动型和键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的故事板一个视图来显示用户登录的形式,所以它看起来是这样的:主视图 - >滚动查看 - >内容查看 - >两个文本字段,并在顶部和一个寄存器键登录按钮视图的底部。我用自动版式和底部的按钮有底部空间限制。当我点击的文本字段,键盘会出现,我想滚动视图改变大小可见矩形,但内容大小应保持向下滚动到注册按钮,但键向上移动时滚动视图更改的大小。我怎么能这样做我想要什么?

我用这个code键盘出现时:

   - (无效)keyboardWillShow:(NSNotification *)aNotification
{
    *的NSDictionary信息= [aNotification USERINFO]
    NSValue * kbFrame = [信息objectForKey:UIKeyboardFrameEndUserInfoKey];
    NSTimeInterval animationDuration = [[信息objectForKey:UIKeyboardAnimationDurationUserInfoKey]的doubleValue];
    的CGRect keyboardFrame = [kbFrame CGRectValue]    CGSize S = self.scrollView.contentSize;
    CGFloat的高度= keyboardFrame.size.height;
    self.scrollViewBottomLayoutConstraint.constant =高度;    [UIView的animateWithDuration:animationDuration动画:^ {
        [self.view layoutIfNeeded]
        [self.scrollView setContentSize:秒]
    }];
}


解决方案

尝试独自离开内容的大小,而是调整滚动视图的contentInset财产。然后,你不必带约束的混乱。

   - (无效)keyboardUp:(NSNotification *)的通知
{
    *的NSDictionary信息= [通知用户信息];
    的CGRect keyboardRect = [[信息objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]
    keyboardRect = [self.view convertRect:keyboardRect fromView:无];    UIEdgeInsets contentInset = self.scrollView.contentInset;
    contentInset.bottom = keyboardRect.size.height;
    self.scrollView.contentInset = contentInset;
}

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. 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. 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;
}

这篇关于iOS的自动版式与滚动型和键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆