首页 > 代码库 > 键盘遮挡输入框处理

键盘遮挡输入框处理

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px "PingFang SC"; color: #008400 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #3e1e81 } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000 } span.s1 { font: 11.0px Menlo; color: #000000 } span.s2 { font: 11.0px Menlo } span.s3 { } span.s4 { color: #000000 } span.s5 { color: #703daa } span.s6 { color: #ba2da2 } span.s7 { color: #3e1e81 }</style> <style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #3e1e81 } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000; min-height: 13.0px } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #703daa } p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px "PingFang SC"; color: #008400 } p.p6 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #31595d } span.s1 { } span.s2 { color: #ba2da2 } span.s3 { color: #000000 } span.s4 { color: #703daa } span.s5 { color: #d12f1b } span.s6 { font: 11.0px "PingFang SC"; color: #d12f1b } span.s7 { color: #4f8187 } span.s8 { color: #3e1e81 } span.s9 { font: 11.0px Menlo; color: #000000 } span.s10 { font: 11.0px Menlo }</style> <style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #4f8187 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #ba2da2 } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000 } span.s1 { color: #ba2da2 } span.s2 { color: #000000 } span.s3 { } span.s4 { color: #703daa }</style>

1.定义一个记录光标底部的成员变量cursorBottom

2.监听键盘出现和键盘消失通知(记得在dealloc方法中 移除监听器)

3.在UITextField的代理方法textFieldShouldBeginEditing:中转换当前输入框的原点坐标到scrollView中的坐标,计算光标底部坐标并保存

4.在监听键盘出现执行方法中计算 需不需要滚动及滚动大小,动画实现滚动

5.在监听键盘消失方法和点击空白处方法,关闭键盘

 

 

主要实现代码如下:

 

@interface AddAccountViewController ()<UITextFieldDelegate>

@property (nonatomic, assign) CGFloat  cursorBottom;

@end

 

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000 } span.s1 { color: #ba2da2 } span.s2 { }</style>

@implementation AddAccountViewController

- (void)viewDidLoad {

    [super viewDidLoad];

 

    

    self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];

    self.scrollView.backgroundColor = self.view.backgroundColor;

    [self.scrollView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hiddenKeyboard)]];

    [self.view addSubview:self.scrollView];

    

    //注册键盘出现的通知

    [[NSNotificationCenter defaultCenter] addObserver:self

                                             selector:@selector(keyboardWillShow:)

                                                 name:UIKeyboardWillShowNotification object:nil];

    //注册键盘消失的通知

    [[NSNotificationCenter defaultCenter] addObserver:self

                                             selector:@selector(keyboardWillHide:)

                                                 name:UIKeyboardWillHideNotification object:nil];

    

    [self setupAccountInfo];

    

}

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #3e1e81 } span.s1 { } span.s2 { color: #ba2da2 } span.s3 { color: #000000 } span.s4 { color: #703daa }</style>

- (void)dealloc

{

    [[NSNotificationCenter defaultCenter] removeObserver:self];

}

 

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #78492a } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000 } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000; min-height: 13.0px } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #ba2da2 } span.s1 { } span.s2 { color: #ba2da2 } span.s3 { color: #703daa } span.s4 { color: #3e1e81 } span.s5 { color: #272ad8 } span.s6 { color: #4f8187 } span.s7 { color: #000000 }</style> <style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #78492a } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000 } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000; min-height: 13.0px } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #ba2da2 } p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px "PingFang SC"; color: #008400 } p.p6 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #703daa } p.p7 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008400 } p.p8 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #3e1e81 } span.s1 { } span.s2 { color: #ba2da2 } span.s3 { color: #703daa } span.s4 { color: #3e1e81 } span.s5 { color: #272ad8 } span.s6 { color: #4f8187 } span.s7 { color: #000000 } span.s8 { font: 11.0px "PingFang SC" } span.s9 { font: 11.0px Menlo } span.s10 { font: 11.0px Menlo; color: #000000 }</style>

#pragma mark UITextFieldDelegate

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField

{

    CGPoint point = [textField convertPoint:CGPointMake(0, 0) toView:_scrollView];

    

   self.cursorBottom = textField.height+point.y ;

    

    return YES;

    

}

 

#pragma mark 键盘代理事件

///键盘显示事件

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

    //获取键盘高度,在不同设备上,以及中英文下是不同的

    CGFloat kbHeight = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;

    

    //计算出键盘顶端到inputTextView panel底端的距离(加上自定义的缓冲距离INTERVAL_KEYBOARD)

    CGFloat offset =  self.cursorBottom + kbHeight - self.view.height;

    

    // 取得键盘的动画时间,这样可以在视图上移的时候更连贯

    double duration = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    

    //将视图上移计算好的偏移

    if(offset > 0) {

        [UIView animateWithDuration:duration animations:^{

            self.scrollView.contentOffset = CGPointMake(0, offset);

        }];

    }

}

 

///键盘消失事件

- (void) keyboardWillHide:(NSNotification *)notify {

    // 键盘动画时间

    double duration = [[notify.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    

    //视图下沉恢复原状

    [UIView animateWithDuration:duration animations:^{

        self.scrollView.contentOffset = CGPointMake(0, 0);

    }];

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

    [self.view endEditing:YES];

}

- (void)hiddenKeyboard

{

    [self.view endEditing:YES];

}

@end

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #78492a } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px "PingFang SC"; color: #008400 } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000 } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #703daa } p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000; min-height: 13.0px } p.p6 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008400 } p.p7 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #3e1e81 } span.s1 { } span.s2 { font: 11.0px "PingFang SC" } span.s3 { font: 11.0px Menlo } span.s4 { color: #ba2da2 } span.s5 { color: #703daa } span.s6 { font: 11.0px Menlo; color: #000000 } span.s7 { color: #000000 } span.s8 { color: #3e1e81 } span.s9 { color: #4f8187 } span.s10 { color: #272ad8 }</style>

键盘遮挡输入框处理