IOS简单实现输入框随着键盘的弹出动态上升(很简单的)

1.视图滑动型的

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField

{
    [UIView animateWithDuration:0.25 animations:^{
        CGRect frame = self.frame;
        frame.origin.y = - 40;
        self.frame = frame;
    }];
    return YES;

}

frame.origin.y输入框的高度 根据自己视图的布局调整数值的大小,但是必须是负数(上升).

实现这个方法必须让输入框(textField)设置代理,另外将view必须继承至UIScrollView,否则没有frame这个属性.

也可以围绕这个方法设置其他的属性(视图不继承UIScrollView)从而实现输入框的上升

//视图恢复原样(只要scrollview滑动就会触发,(触发多次))

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{

   if (0 != scrollView.frame.origin.y) {

        [(AddAddressPersonView *)scrollViewresignKeyboard];//键盘回收(自定义方法实质为[textFieldresignFirstResponder])

        [UIViewanimateWithDuration:0.25animations:^{

            scrollView.frame = [UIScreenmainScreen].bounds;

        }];

    }

}

2.视图不滑动的(记得设置代理)

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [UIView animateWithDuration:0.25 animations:^{
        CGRect frame = self.view.frame;
        frame.origin.y = -40;
        self.view.frame = frame;
    }];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [UIView animateWithDuration:0.25 animations:^{
        self.view.frame = [UIScreen mainScreen].bounds;
    }];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.tf resignFirstResponder];
    [self.textView resignFirstResponder];
}


你可能感兴趣的:(代码库)