自动隐藏软键盘,分为两步,一个是单击软键盘外部任意空间;另外一个是单击软键盘上的return键。下面依次实现
单击软键盘外部空间键隐藏软键盘:
一:在viewDidLoad中添加一个UITabGestureRecognizer,并将其添加到ViewController的view对象上
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)]; [self.view addGestureRecognizer:tap];
二:可以看到上面有一个dismissKeyboard的selector,下面实现该方法,并在方法中通过resignFirstResponder来隐藏软键盘
-(void)dismissKeyboard { [feedbackTextField resignFirstResponder]; }
单击软键盘上的return键隐藏软键盘: 一:使ViewController实现UITextFieldDelegate协议,ViewController.h
// // FeedBackViewController.h // YofotoEdu // // Created by wly on 13-10-14. // Copyright (c) 2013年 yofoto. All rights reserved. // #import <UIKit/UIKit.h> @interface FeedBackViewController : UIViewController <UITextFieldDelegate> @property (strong, nonatomic) IBOutlet UITextField *feedbackTextField; @end
二:在ViewDidLoad为UITextFiled指定delegate
feedbackTextField.delegate = self;
三:实现UITextFieldDelegate中的textFieldShouldReturn:方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField { [feedbackTextField resignFirstResponder]; }
O啦~~~
转载请保留出处:http://blog.csdn.net/u011638883/article/details/12772279
谢谢!!
转载于:https://www.cnblogs.com/riasky/p/3372014.html