鍵盤遮擋輸入框問題IQKeyboardManager 自動調(diào)整頁面位置的鍵盤的框架

在開發(fā)中,可能會遇到一個頁面好多textField或者textView的情況,鍵盤彈出時,會遮蓋下面的輸入框,使我們看不到內(nèi)容,以往我都是測量當前textfield在屏幕中的位置,current_y,及current_height,計算當前鍵盤的height是否比當前textfield的下方位置要高,如果高的話,則需調(diào)整self.view整體位置。詳見文章鏈接? http://m.itdecent.cn/p/82881c4659b8? 這個方法比較麻煩,現(xiàn)在我們引入IQKeyboardManger就能輕松解決我們的問題

1 . 首先導入IQKeyboardManager框架

$? pod 'IQKeyboardManager'

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

#import "IQKeyboardManager.h"

3.創(chuàng)建如圖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;

}

結(jié)果如下,鍵盤不會遮擋輸入框,頁面整體自動上調(diào),大功告成?。?!

圖2
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容