http://www.reibang.com/p/9d7d246bd350
IQKeyboardManager是iOS中解決鍵盤彈起遮擋UITextField/UITextView的一種很實(shí)用的工具。無需輸入任何代碼,不需要額外的設(shè)置握恳。使用IQKeyboardManager的你只需要將源文件添加到項(xiàng)目瞒窒。
這里附上下載鏈接:https://github.com/hackiftekhar/IQKeyboardManager
IQKeyboardManager支持Objective-C和swift,這里建議實(shí)用Cocoapod安裝乡洼,很方便管理崇裁。
簡(jiǎn)單總結(jié)一下IQKeyboardManager最重要的功能有以下幾點(diǎn):
1.當(dāng)鍵盤遮擋輸入文本框的時(shí)候,自動(dòng)上移View束昵,使被遮擋的部分自動(dòng)處于鍵盤的上方拔稳。
function1.gif
)
導(dǎo)入IQKeyboardManager后,默認(rèn)所有的頁面都有了這個(gè)功能锹雏,如果你在哪一個(gè)界面不想有這個(gè)效果可以在當(dāng)前界面控制器的生命周期方法中進(jìn)行設(shè)置:
#import
-
(void)viewWillAppear:(BOOL)animated{
[super
viewWillAppear:animated];
[IQKeyboardManager
sharedManager].enable = NO;
}
-
(void)viewWillDisappear:(BOOL)animated{
[super
viewWillDisappear:animated];
[IQKeyboardManager
sharedManager].enable = YES;
}
或者在
AppDelegate中注冊(cè)方法:
-
(BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[IQKeyboardManager
sharedManager] disableInViewControllerClass:[ViewController class]];
returnYES;
}
2.在鍵盤上會(huì)自動(dòng)添加一個(gè)工具條巴比,工具條上有左箭頭和右箭頭用來切換的輸入文本框,還有完成按鈕用來收回鍵盤。
function2.gif
你也可以不使用這個(gè)自動(dòng)工具條轻绞,設(shè)置的代碼如下:
[IQKeyboardManagersharedManager].enableAutoToolbar = NO;
3.可以將鍵盤上的return按鍵采记,變?yōu)镹ext/Done按鍵,默認(rèn)最后一個(gè)UITextField/UITextView的鍵盤return鍵變?yōu)镈one政勃。
順序是按照創(chuàng)建控件的先后順序唧龄,而不是從上到下的擺放順序。
function3.gif
設(shè)置的代碼如下:
#import
@implementationViewController
{
IQKeyboardReturnKeyHandler* returnKeyHandler;
}
-(void)viewDidLoad
{
[super viewDidLoad];
returnKeyHandler= [[IQKeyboardReturnKeyHandler alloc] initWithViewController:self];
}
4.設(shè)置點(diǎn)擊背景收回鍵盤奸远。
-(void)viewDidLoad {
[super viewDidLoad];
[IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
}
5.如果你的視圖有導(dǎo)航欄既棺,你不想上移View時(shí),UINavigationBar消失,如function3.gif所示懒叛,你也可以進(jìn)行相應(yīng)設(shè)置:
如果你使用的是storyboard or xib丸冕,只需將當(dāng)前視圖視圖控制器中的UIView class變?yōu)閁IScrollView。
如果你使用的是代碼薛窥,你就需要覆蓋UIViewController中的'-(void)loadView'
方法:
-(void)loadView
{
UIScrollView *scrollView =[[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.view = scrollView;
}
原文鏈接:http://www.reibang.com/p/9d7d246bd350