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

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

1 . 首先導(dǎo)入IQKeyboardManager框架

$? pod 'IQKeyboardManager'

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

#import "IQKeyboardManager.h"

3.創(chuàng)建如圖1 的? 幾個(gè)textField

圖1

輸入文字時(shí),鍵盤會(huì)遮擋部分輸入框

? ? 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. 具體設(shè)置如下

//設(shè)置IQKeyboardManager

//1 控制自動(dòng)鍵盤功能啟用與否,默認(rèn)為YES (只要項(xiàng)目中導(dǎo)入了此框架,不用設(shè)置下面代碼,也會(huì)自動(dòng)開啟 )

[IQKeyboardManager sharedManager].enable = YES;

//2? 鍵盤彈出時(shí),點(diǎn)擊背景,鍵盤回收

[IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;

//3? 隱藏鍵盤上的toolbar,默認(rèn)是開啟的

[IQKeyboardManager sharedManager].enableAutoToolbar = NO;

//4? 如果某一個(gè)m文本框確實(shí)不需要鍵盤上面的toolbar(置空即可)

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

//5. 如果某個(gè)頁(yè)面一進(jìn)來不想讓鍵盤彈出

- (void)viewWillAppear:(BOOL)animated{

? ? [superviewWillAppear:animated];

? ? //關(guān)閉自動(dòng)鍵盤功能

? [IQKeyboardManager sharedManager].enable = YES;

}

- (void)viewWillDisappear:(BOOL)animated{

? ? [super viewWillDisappear:animated];

? //開啟自動(dòng)鍵盤功能

? [IQKeyboardManager sharedManager].enable = YES;

}

結(jié)果如下,鍵盤不會(huì)遮擋輸入框,頁(yè)面整體自動(dòng)上調(diào),大功告成?。。?/p>

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

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

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