隐藏键盘方法总结

当我们在可输入信息的控件上结束输入时,可隐藏键盘以下两句代码:

1 [self.onetf resignFirstResponder]; 某一个可输入控件放弃第一响应者

2 [self.view endEditing:YES]; 当前页面上所有可输入控件停止编辑


用以下几个方法来隐藏键盘1 借助当前输入框的父View(或者父View的父View)

1)父view为继承自UIResponder的对象

可以直接调用touch方法来隐藏键盘

-(void)touchesEnded:(NSSet*)touches withEvent:(UIEvent *)event {

[self.onetf resignFirstResponder];

}

2)在父view上添加点击事件

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismissKeyboard:)];

[self.fristView addGestureRecognizer:tap];

- (void)dismissKeyboard:(id)sender{

[self.onetf resignFirstResponder];

}

3)将父view所继承的类改为UIControl,使用UIControl 的 Touch Up Inside 事件来收起键盘


隐藏键盘方法总结_第1张图片

- (IBAction)dismissKeyboard:(id)sender {

[self.onetf resignFirstResponder];

}

2 点击键盘上的Return键 收起键盘

- (BOOL)textFieldShouldReturn:(UITextField *)textField {

[self.onetf resignFirstResponder];

return YES;

}

3 继承于UIScrollView的控件可以在滑动是收起键盘

1 )首先遵守UIScrollViewDelegate协议,滚动结束后收起键盘

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{

[self.onetf resignFirstResponder];

}

2)设置ScrollView的属性keyboardDismissMode,滚动时收起键盘

self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;

4 统一收起键盘

 [[[UIapplication sharedApplication] keyWindow] endEditing:YES];

5. 直接发送 resignFirstResponder 消息:

[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];

你可能感兴趣的:(隐藏键盘方法总结)