如何在iOS Obj-C中隐藏键盘触摸UITableView
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在iOS Obj-C中隐藏键盘触摸UITableView相关的知识,希望对你有一定的参考价值。
我是ios开发的新手。我想在TextField
外面敲击时隐藏键盘。我的TextField
在UITableView
的一个牢房里。
我试图关注其中的一些链接,但没有任何成功 -
Dismiss keyboard on touch anywhere outside UITextField
Dismiss keyboard by touching background of UITableView
Hide keyboard when scroll UITableView
我试图找到最简单的方法。提前致谢
这是解除键盘的最简单方法
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
[tableView addGestureRecognizer:gestureRecognizer];
}
- (void)hideKeyboard
{
[self.view endEditing:YES];
}
这不是关于触摸,只是在滚动时工作
TableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
也有
UIScrollViewKeyboardDismissModeInteractive //键盘跟随拖动触摸屏幕,并可能再次向上拉以取消关闭
添加委托类UITextFieldDelegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
您需要添加一个UITapGestureRecogniser并将其分配给视图,然后在其选择器的文本字段上调用resign first responder。
你可以使用Tap手势来隐藏键盘。
- (void) tapGesture : (UIGestureRecognizer *) gestureRecognizer {
for (UIView *subview in view.subviews) {
if([subview isKindOfClass : [UITextField class]] ) {
UITextField *tf = (UITextField *) subview;
[tf resignFirstResponder];
}
}
}
尝试此代码在viewDidLoad中编写以下代码,并在.h文件中添加UIGestureRecognizerDelegate。
UITapGestureRecognizer *singleFingerTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(handleSingleTap:)];
[singleFingerTap setDelegate:self];
[self.view addGestureRecognizer:singleFingerTap];
// Listen for keyboard appearances and disappearances
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidHide:)
name:UIKeyboardDidHideNotification
object:nil];
键盘代表出现和失踪
- (void)keyboardDidShow: (NSNotification *) notif{
// Do something here
tblview.tag = 1;
}
- (void)keyboardDidHide: (NSNotification *) notif{
// Do something here
tblview.tag = 0;
}
用于隐藏键盘的UITapGestureRecognizer事件函数
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
blview.tag = 0;
[self.view endEditing:YES];
}
UIGestureRecognizer委托
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
if(tblview.tag == 1){
return TRUE;
}
else{
return FALSE;
}
}
我将解决方案分为两部分:
要在tableview / collectionview上关闭键盘,请点击:
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
gestureRecognizer.cancelsTouchesInView= NO;
[self.collectionView addGestureRecognizer:gestureRecognizer];
(不要忘记cancelsTouchesInView
设置为NO
以获取tableview / collection视图的触摸事件)
要在滚动时关闭键盘(因为tableview / collectionview是UIScrollView的子类):
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
[self.view endEditing:YES];
}
希望它对某人有帮助。
这会对你有所帮助..
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
最简单的方法是在viewDidLoad中分配一个tap Gesture,然后隐藏键盘
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
[_tableView addGestureRecognizer:gestureRecognizer];
}
- (void)hideKeyboard
{
[self.view endEditing:YES];
}
或者在github上你肯定找到了一个隐藏键盘的库
以上是关于如何在iOS Obj-C中隐藏键盘触摸UITableView的主要内容,如果未能解决你的问题,请参考以下文章