/** 方法一:点击return是键盘隐藏
创建的输入框需要设置代理,并让代理遵守这个协议
@interface ServieProgressView ()<UITextFieldDelegate>
UITextField *textField = [[UITextField alloc]init];
textField.delegate = self;
*/
#pragma mark - <UITextFieldDelegate>
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
// 当点击return键的时候会走这个方法
[textField resignFirstResponder]; //直接取消目前输入框的第一响应就行了
return YES;
}
#pragma mark - <Touches methods>
/** 方法二:点击背景实现键盘隐藏
*/
// 通过点击事件来实现 只需重写它让view 结束编辑事件
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
<pre name="code" class="objc">#pragma mark - 注册键盘通知 -
// 注册这些系统通知,一般是对当弹出的键盘把输入框遮住了,通过通知让界面上移从而让输入框不被遮住 当输入结束又让界面下移就行了
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillAppear)//当键盘出现的时候需要观察者响应的方法
name:UIKeyboardWillShowNotification//观察键盘出现事件
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillDisappear)
name:UIKeyboardWillHideNotification//观察键盘消失事件
object:nil];