第三方真是個好東西,搜索IQKeyboardManager,這個第三方能夠幫咱們完美的解決鍵盤遮擋的問題,使用的方法也很是簡單,能夠使用cocoapods直接搜索IQKeyboardManager,會出現兩個,一個是OC的,另外一個是swift的,你們看着選,別弄錯了,咱們也能夠從GotHub上下載下來,鏈接是https://github.com/hackiftekhar/IQKeyboardManager,而後把IQKeyboardManager文件夾拖入工程,下面咱們就開始正式的使用:git
第一步:在AppDelegate.m中引入頭文件IQKeyboardManager.h,而後在github
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中加入如下幾行代碼:swift
即:app
1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 2 // Override point for customization after application launch. 3 4 // 加入如下幾行代碼,實現鍵盤遮擋 5 IQKeyboardManager* manager = [IQKeyboardManager sharedManager]; 6 manager.enable=YES; 7 manager.shouldResignOnTouchOutside=YES; 8 manager.shouldToolbarUsesTextFieldTintColor=YES; 9 manager.enableAutoToolbar=YES; 10 11 12 return YES; 13 }
第二步:在你要實現遮擋的文件中引入IQKeyboardReturnKeyHandler.h頭文件,而後在- (void)viewDidLoad方法中加入如下兩行代碼,ide
即:測試
1 - (void)viewDidLoad { 2 [super viewDidLoad]; 3 // Do any additional setup after loading the view, typically from a nib. 4 5 // 加入如下兩行代碼,用來實現鍵盤遮擋 6 IQKeyboardReturnKeyHandler *returnKeyHandler = [[IQKeyboardReturnKeyHandler alloc]init]; 7 returnKeyHandler.lastTextFieldReturnKeyType = UIReturnKeyNext; 8 }
通過以上兩步,就實現了第三方的引用,此時,咱們就能夠在視圖控制器上的任意位置建立textField了,通過測試,咱們能夠發現,真的實現了哎,一言以蔽之:第三方真他媽好!spa