键盘遮挡输入框问题IQKeyboardManager 自动调整页面位置的键盘的框架

在开发中,可能会遇到一个页面好多textField或者textView的情况,键盘弹出时,会遮盖下面的输入框,使我们看不到内容,以往我都是测量当前textfield在屏幕中的位置,current_y,及current_height,计算当前键盘的height是否比当前textfield的下方位置要高,如果高的话,则需调整self.view整体位置。详见文章链接  https://www.jianshu.com/p/82881c4659b8  这个方法比较麻烦,现在我们引入IQKeyboardManger就能轻松解决我们的问题

1 . 首先导入IQKeyboardManager框架

$  pod 'IQKeyboardManager'

2.安装完成之后,引入文件,最好放在项目的PrefixHeader文件中

#import "IQKeyboardManager.h"

3.创建如图1 的  几个textField

图1

输入文字时,键盘会遮挡部分输入框

    UITextField * tf = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 200, 60)];

    tf.placeholder = @"please input placeholder words.";

    UITextField * tf2 = [[UITextField alloc]initWithFrame:CGRectMake(100, 240, 200, 60)];

    tf2.placeholder = @"please input placeholder words.";

    UITextField* tf3 = [[UITextFieldalloc]initWithFrame:CGRectMake(100,320,200,60)];

    tf3.placeholder = @"please input placeholder words.";

    UITextField* tf4 = [[UITextFieldalloc]initWithFrame:CGRectMake(100,400,200,60)];

    tf4.placeholder = @"please input placeholder words.";

    UITextField * tf5 = [[UITextField alloc]initWithFrame:CGRectMake(100, 480, 200, 60)];

    tf5.placeholder = @"please input placeholder words.";

    tf.borderStyle = UITextBorderStyleRoundedRect;

    tf2.borderStyle = UITextBorderStyleRoundedRect;

    tf3.borderStyle = UITextBorderStyleRoundedRect;

    tf4.borderStyle = UITextBorderStyleRoundedRect;

    tf5.borderStyle = UITextBorderStyleRoundedRect;

    [self.viewaddSubview:tf];

    [self.viewaddSubview:tf5];

    [self.viewaddSubview:tf2];

    [self.viewaddSubview:tf3];

    [self.viewaddSubview:tf4];


3. 具体设置如下

//设置IQKeyboardManager

//1 控制自动键盘功能启用与否,默认为YES (只要项目中导入了此框架,不用设置下面代码,也会自动开启 )

[IQKeyboardManager sharedManager].enable = YES;

//2  键盘弹出时,点击背景,键盘回收

[IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;

//3  隐藏键盘上的toolbar,默认是开启的

[IQKeyboardManager sharedManager].enableAutoToolbar = NO;

//4  如果某一个m文本框确实不需要键盘上面的toolbar(置空即可)

    tf.inputAccessoryView = [[UIView alloc]init];

//5. 如果某个页面一进来不想让键盘弹出

- (void)viewWillAppear:(BOOL)animated{

    [superviewWillAppear:animated];

    //关闭自动键盘功能

  [IQKeyboardManager sharedManager].enable = YES;

}

- (void)viewWillDisappear:(BOOL)animated{

    [super viewWillDisappear:animated];

  //开启自动键盘功能

  [IQKeyboardManager sharedManager].enable = YES;

}

结果如下,键盘不会遮挡输入框,页面整体自动上调,大功告成!!!

图2

你可能感兴趣的:(键盘遮挡输入框问题IQKeyboardManager 自动调整页面位置的键盘的框架)