设置输入时页面适应键盘展示

步骤1.viewDidLoad{}方法中设置监听

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

步骤2.在.m中添加此方法,此时已经设置结束编辑时(收起键盘时页面位置)

- (void)revertViewBounds
{
    if (self.view.bounds.origin.y > 0)
    {
        [UIView animateWithDuration:0.2 animations:^{
            self.view.bounds = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
        }];
    }
}

步骤3.设置开始编辑时页面位置

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{ 
    //可以在此处设置弹出键盘时页面布局
    CGRect newTFRect = [textField convertRect:textField.bounds toView:self.view];
    CGFloat dist = (ScreenHeight - 216) - (newTFRect.origin.y + newTFRect.size.height + 100);

    if (dist < 0) {
        [UIView animateWithDuration:0.3 animations:^{
            self.view.bounds = CGRectMake(0, -dist, self.view.bounds.size.width, self.view.bounds.size.height);
        }];
    }

    return YES;
} 

你可能感兴趣的:(设置输入时页面适应键盘展示)