通过触摸UITableView的背景来关闭键盘

时间:2010-02-23 19:26:50

标签: ios uitableview uikeyboard

我有UITableViewUITextField s作为单元格。我想触摸UITableView的背景时关闭键盘。我试图通过创建UIButton UITableView的大小并将其放在UITableView后面来尝试这样做。唯一的问题是即使触摸在UITableView上,UIButton也能捕获所有触摸。我做错了什么?

谢谢!

29 个答案:

答案 0 :(得分:197)

这可以通过创建UITapGestureRecognizer对象轻松完成(默认情况下,这将在单击时检测“手势”,因此无需进一步自定义),指定触发手势时的目标/操作,然后将手势识别器对象附加到表视图。

E.g。也许在你的viewDidLoad方法中:

UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
[self.tableView addGestureRecognizer:gestureRecognizer];

hideKeyboard方法可能如下所示:

- (void) hideKeyboard {
    [textField1 resignFirstResponder];
    [textField2 resignFirstResponder];
    ...
    ...
}

请注意,触摸UITextField对象内部时不会触发手势。它虽然在UITableView背景,页脚视图,标题视图和单元格内的UILabels等上被触发。

答案 1 :(得分:124)

如果设置:

,UITapGestureRecognizer解决方案适用于表格单元格选择
gestureRecognizer.cancelsTouchesInView = NO;

答案 2 :(得分:61)

这是执行此操作的最佳方式。 就这样做

[self.view endEditing:YES];

[[self.tableView superView] endEditing:YES];

答案 3 :(得分:51)

您也可以从Storyboard中执行此操作: enter image description here

答案 4 :(得分:22)

由于UITableViewUIScrollView的子类,因此在下面实现一个委托方法提供了一种非常简单,快速的解决方案。甚至不需要涉及resignFirstResponder,因为视图层次结构内省并找到当前响应者并要求它重新签署响应者状态。

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.view endEditing:YES];
}

请记住将UIScrollViewDelegate添加到头文件中。

答案 5 :(得分:13)

首先,通过添加scrollViewWillBeginDragging来监听UIViewController中的UIScrollViewDelegate

在.h文件中:

@interface MyViewController : UIViewController <UIScrollViewDelegate> 

在.m文件中:

- (void)scrollViewWillBeginDragging:(UIScrollView *)activeScrollView {

    [self dismissKeyboard];

}

然后听取其他互动:

- (void)setupKeyboardDismissTaps {

    UISwipeGestureRecognizer *swipeUpGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
    swipeUpGestureRecognizer.cancelsTouchesInView = NO;
    swipeUpGestureRecognizer.direction = UISwipeGestureRecognizerDirectionUp;
    [self.tableView addGestureRecognizer:swipeUpGestureRecognizer];

    UISwipeGestureRecognizer *swipeDownGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
    swipeDownGestureRecognizer.cancelsTouchesInView = NO;
    swipeDownGestureRecognizer.direction = UISwipeGestureRecognizerDirectionDown;
    [self.tableView addGestureRecognizer:swipeDownGestureRecognizer];

    UISwipeGestureRecognizer *swipeLeftGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
    swipeLeftGestureRecognizer.cancelsTouchesInView = NO;
    swipeLeftGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.tableView addGestureRecognizer:swipeLeftGestureRecognizer];

    UISwipeGestureRecognizer *swipeRightGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
    swipeRightGestureRecognizer.cancelsTouchesInView = NO;
    swipeRightGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
    [self.tableView addGestureRecognizer:swipeRightGestureRecognizer];


    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
    tapGestureRecognizer.cancelsTouchesInView = NO;
    [self.tableView addGestureRecognizer:tapGestureRecognizer];

}

然后实施dismissKeyboard

- (void)dismissKeyboard {

    NSLog(@"dismissKeyboard");

    [yourTextFieldPointer resignFirstResponder];

}

如果像我一样,你想在自定义表格单元格中解除UITextField的键盘:

- (void)dismissKeyboard {

    NSLog(@"dismissKeyboard");

    CustomCellClass *customCell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    [customCell.textFieldInCell resignFirstResponder]; 

}

希望能帮助任何人搜索!!

答案 6 :(得分:9)

tableView.keyboardDismissMode = .onDrag

答案 7 :(得分:7)

我是这样做的:

在TableViewController中创建一个方法来停用第一个响应者(此时将是你的TextBox)

- (BOOL)findAndResignFirstResonder:(UIView *)stView {
    if (stView.isFirstResponder) {
        [stView resignFirstResponder];
        return YES;     
    }

    for (UIView *subView in stView.subviews) {
        if ([self findAndResignFirstResonder:subView]) {
            return YES;
        }
    }
    return NO;
}

tableView:didSelectRowAtIndexPath:中调用上一个方法:

- (void)tableView:(UITableView *)tableView
                             didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ...
    [self findAndResignFirstResonder: self.view];
    ...
}

答案 8 :(得分:7)

这是您的编码乐趣的快速版本:

它添加了一个轻敲手势识别器然后解除键盘。不需要TextField的插座!

override func viewDidLoad() {
    super.viewDidLoad()
    view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "handleTap:"))
}

func handleTap(sender: UITapGestureRecognizer) {
    if sender.state == .Ended {
        view.endEditing(true)
    }
    sender.cancelsTouchesInView = false
}

答案 9 :(得分:5)

Swift 3版本没有阻止单元格上的点击。

viewDidLoad()方法中:

let dismissKeyboardGesture = UITapGestureRecognizer(target: self, action: #selector(hideKeyboard))
dismissKeyboardGesture.cancelsTouchesInView = false
tableView.addGestureRecognizer(dismissKeyboardGesture)

hideKeyboard看起来像这样:

func hideKeyboard() {
    view.endEditing(true)
}

答案 10 :(得分:5)

我有一个UITableViewController并且实施touchesBegan:withEvent:并不适合我。

以下是有效的:

斯威夫特:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    view.endEditing(true)
}

目标-C:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.view endEditing:YES];
}

答案 11 :(得分:4)

@interface DismissableUITableView : UITableView {
}
@end

@implementation DismissableUITableView

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
 [self.superview endEditing:YES];
 [super touchesBegan:touches withEvent:event];
}

@end

然后确保在您的Nib文件中将UITableView的类型设置为DismissableUITableView .....也许我可以想到这个类的更好的名称,但是你明白了。

答案 12 :(得分:4)

如果您的目标是iOS7,则可以使用以下方法之一:

tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;

tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;

前者会在滚动表格视图时将键盘设置为关闭屏幕,而后者会像股票消息应用程序一样隐藏键盘。

请注意,这些来自UIScrollViewUITableView继承自。{/ p>

答案 13 :(得分:2)

UITableView是UIScrollView的子类。

我这样做的方法是由用户监听滚动事件,然后是resignFirstResponder。这是在代码中实现的UIScrollViewDelegate方法;

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

当我接近这些问题时,我发现最好的方法是研究每个对象和父类的委托协议(在本例中为UITableViewDelegate,UIScrollViewDelegate.NS对象触发的事件数量非常大,全面的。实现协议然后继承任何东西也更容易。

答案 14 :(得分:2)

我有同样的问题,这是我的解决方案,它对我来说很完美:

在您实施<UITextFieldDelegate>

的视图或视图控制器中

(在我的情况下,我有一个名为TextFieldCell的自定义UITableViewCell

UITapGestureRecognizer声明为属性:

@interface TextFieldCell : UITableViewCell <UITextFieldDelegate>
{
    UITextField *theTextField;
    UITapGestureRecognizer *gestureRecognizer;
}
@property (nonatomic,retain) UITextField *theTextField;
@property (nonatomic,retain) UITapGestureRecognizer *gestureRecognizer; 

并在您的视图/控制器中初始化它:

self.gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeKeyboard:)];

- (void)textFieldDidBeginEditing:(UITextField *)textField方法中,使用superView向上移动到tableView并调用addGestureRecognizer

[self.superview.superview addGestureRecognizer:gestureRecognizer];

- (void)textFieldDidEndEditing:(UITextField *)textField中,只需删除手势识别器:

[self.superview.superview removeGestureRecognizer:gestureRecognizer];

希望它有所帮助。

答案 15 :(得分:2)

我希望我的单元格在选择单元格的任何部分时打开键盘,如果单击单元格的任何位置,则关闭它。要打开键盘:

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    if (selected)
    {
        [self.textField becomeFirstResponder];
    }
}

(注意:我已将该单元格子类化,但您可以使用tableView:didSelectRowAtIndexPath:的{​​{1}}委托方法轻松实现此目的

这样做意味着使用顶级解决方案,如果您点击两次单元格键盘会摇晃,首先手势识别器尝试关闭键盘,然后重新选择单元格并尝试打开键盘。

解决方法是检查点击是否发生在当前选定的单元格内:

UITableView

答案 16 :(得分:2)

我找到了一个效果很好的解决方案。

需要使用 UIGestureRecognizerDelegate 和方法 - gestureRecognizer:shouldReceiveTouch:

将手势识别器添加到TableView,如下所示:

UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
tapGestureRecognizer.cancelsTouchesInView = NO;
tapGestureRecognizer.delegate = self;
[self.suggestedTableView addGestureRecognizer:tapGestureRecognizer];
[tapGestureRecognizer release];

然后,实现 shouldReceiveTouch 委托方法来拒绝在UITableViewCell类中执行的触摸。仅在UITableViewCell类之外执行触摸时,才会调用 hideKeyboard 方法。

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if([touch.view isKindOfClass:[UITableViewCell class]]) {
        return NO;
    }
    // UITableViewCellContentView => UITableViewCell
    if([touch.view.superview isKindOfClass:[UITableViewCell class]]) {
        return NO;
    }
    // UITableViewCellContentView => UITableViewCellScrollView => UITableViewCell
    if([touch.view.superview.superview isKindOfClass:[UITableViewCell class]]) {
        return NO;
    }
    return YES; // handle the touch
}

- (void) hideKeyboard{
    [textField resignFirstResponder];
}

答案 17 :(得分:1)

tableView.keyboardDismissMode = .onDrag // .interactive

答案 18 :(得分:1)

简单地使用UITapGestureRecognizer和cancelsTouchesInView = NO意味着点击单元格和UITextViews也会触发隐藏。如果您有多个UITextView并且点击下一个UITextView,那就不好了。键盘将开始隐藏,然后下一个textView成为firstResponder,键盘再次可见。要避免这种情况,请检查分接位置,如果分接头不在单元格上,则仅隐藏键盘:

// init
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapTableView:)];
tapRecognizer.cancelsTouchesInView = NO;
[self.tableView addGestureRecognizer:tapRecognizer];


// Hide on tap
- (void)didTapTableView:(UITapGestureRecognizer *)tap
{
    CGPoint point = [tap locationInView:tap.view];
    [self.view endEditing:!CGRectContainsPoint([self.tableView rectForRowAtIndexPath:[self.tableView indexPathForRowAtPoint:point]], point)];
}

为了触发scrollViewWillBeginDragging:,tableView的scrollEnabled属性必须为YES

// Hide on scroll
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.view endEditing:YES];
}

答案 19 :(得分:1)

我正在寻找解决方案而没有找到任何适合我的代码的东西,所以我这样做了:

http://82517.tumblr.com/post/13189719252/dismiss-keyboard-on-uitableview-non-cell-tap

它基本上是前面提到的方法的组合,但不需要子类化任何东西或创建背景按钮。

答案 20 :(得分:0)

以下是我最终的作品。我结合了不同答案的建议和代码。 功能:关闭键盘,在编辑时移动键盘上方的文本字段,并设置“下一步”和“完成”键盘返回类型。更多字段的“替换”...

static const CGFloat ANIMATION_DURATION = 0.4;
static const CGFloat LITTLE_SPACE = 5;
CGFloat animatedDistance;
CGSize keyboardSize;

@interface ViewController () <UITextFieldDelegate>
 @property (weak, nonatomic) IBOutlet UITextField *firstNameTXT;
  .....// some other text fields
 @property (weak, nonatomic) IBOutlet UITextField *emailTXT;
@end

@implementation ViewController
- (void)viewDidLoad{
.....
// add tap gesture to help in dismissing keyboard
UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc]
                                       initWithTarget:self
                                       action:@selector(tapScreen:)];// outside textfields

[self.view addGestureRecognizer:tapGesture];

// set text fields return key type to Next, last text field to Done
[self.firstNameTXT setReturnKeyType:UIReturnKeyNext];
.....
[self.emailTXT setReturnKeyType:UIReturnKeyDone];

// set text fields tags
[self.firstNameTXT setTag:0];
....// more text fields
[self.emailTXT setTag:5];

// add keyboard notification
[[NSNotificationCenter defaultCenter] addObserver:self     selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
}
[[NSNotificationCenter defaultCenter] addObserver:self      selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
}

// dismiss keyboard when tap outside text fields
- (IBAction)tapScreen:(UITapGestureRecognizer *)sender {
  if([self.firstNameTXT isFirstResponder])[self.firstNameTXT resignFirstResponder];
  ...
  if([self.emailTXT isFirstResponder])[self.emailTXT  resignFirstResponder];

  }
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
   if(textField.returnKeyType==UIReturnKeyNext) {
     // find the text field with next tag
     UIView *next = [[textField superview] viewWithTag:textField.tag+1];
     [next becomeFirstResponder];
   } else if (textField.returnKeyType==UIReturnKeyDone || textField.returnKeyType==UIReturnKeyDefault) {
    [textField resignFirstResponder];
 }
return YES;
}

// Moving current text field above keyboard
-(BOOL) textFieldShouldBeginEditing:(UITextField*)textField{
   CGRect viewFrame = self.view.frame;
   CGRect textFieldRect = [self.view.window convertRect:textField.bounds fromView:textField];
   CGRect viewRect = [self.view.window convertRect:self.view.bounds fromView:self.view];
   CGFloat textFieldBottomLine = textFieldRect.origin.y + textFieldRect.size.height + LITTLE_SPACE;//

   CGFloat keyboardHeight = keyboardSize.height;

   BOOL isTextFieldHidden = textFieldBottomLine > (viewRect.size.height - keyboardHeight)? TRUE :FALSE;
  if (isTextFieldHidden) {
    animatedDistance = textFieldBottomLine - (viewRect.size.height - keyboardHeight) ;
    viewFrame.origin.y -= animatedDistance;
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:ANIMATION_DURATION];
    [self.view setFrame:viewFrame];
    [UIView commitAnimations];
  }
  return YES;
}

-(void) restoreViewFrameOrigionYToZero{
  CGRect viewFrame = self.view.frame;
  if (viewFrame.origin.y != 0) {
    viewFrame.origin.y = 0;
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:ANIMATION_DURATION];
    [self.view setFrame:viewFrame];
    [UIView commitAnimations];
  }
}

-(void)keyboardDidShow:(NSNotification*)aNotification{
   NSDictionary* info = [aNotification userInfo];
   keyboardSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
 }

-(void)keyboardDidHide:(NSNotification*)aNotification{
   [self restoreViewFrameOrigionYToZero];// keyboard is dismissed, restore frame view to its  zero origin
}
@end

答案 21 :(得分:0)

@ mixca的回答非常有用,但如果我和UITextField有什么不同的话。我认为通过使用递归函数搜索主视图的所有子视图来处理它的最佳方法,请查看下面的示例

- (BOOL)findAndResignFirstResponder {
if (self.isFirstResponder) {
    [self resignFirstResponder];
    return YES;
}

    for (UIView *subView in self.subviews) {
        if ([subView findAndResignFirstResponder]) {
            return YES;
        }
    }
    return NO;
}

你也可以把这个方法放到你的实用工具类中,并且可以使用@ mixca的答案等轻敲手势。

答案 22 :(得分:0)

许多有趣的答案。我想在我认为最适合UITableView场景的解决方案中编译不同的方法(这是我通常使用的方法): 我们通常想要的基本上是在两种情况下隐藏键盘:点击文本UI元素外部,或向下/向上滚动UITableView。我们可以通过TapGestureRecognizer轻松添加第一个场景,第二个场景通过UIScrollViewDelegate scrollViewWillBeginDragging:方法添加。 第一项业务,隐藏键盘的方法:

   /**
     *  Shortcut for resigning all responders and pull-back the keyboard
     */
    -(void)hideKeyboard
    {
        //this convenience method on UITableView sends a nested message to all subviews, and they resign responders if they have hold of the keyboard
        [self.tableView endEditing:YES];

    }

此方法会在UITableView视图层次结构中重新分配子视图的任何textField UI,因此它比独立地重新分配每个元素更实用。

接下来,我们通过外部Tap手势解决问题:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupKeyboardDismissGestures];

}

- (void)setupKeyboardDismissGestures
{

//    Example for a swipe gesture recognizer. it was not set-up since we use scrollViewDelegate for dissmin-on-swiping, but it could be useful to keep in mind for views that do not inherit from UIScrollView
//    UISwipeGestureRecognizer *swipeUpGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
//    swipeUpGestureRecognizer.cancelsTouchesInView = NO;
//    swipeUpGestureRecognizer.direction = UISwipeGestureRecognizerDirectionUp;
//    [self.tableView addGestureRecognizer:swipeUpGestureRecognizer];

    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
    //this prevents the gestureRecognizer to override other Taps, such as Cell Selection
    tapGestureRecognizer.cancelsTouchesInView = NO;
    [self.tableView addGestureRecognizer:tapGestureRecognizer];

}

将tapGestureRecognizer.cancelsTouchesInView设置为NO是为了避免gestureRecognizer覆盖UITableView的正常内部工作(例如,不干扰单元格选择)。

最后,要处理在向上/向下滚动UITableView时隐藏键盘,我们必须实现UIScrollViewDelegate协议scrollViewWillBeginDragging:方法,如下:

.h文件

@interface MyViewController : UIViewController <UIScrollViewDelegate>

.m文件

#pragma mark - UIScrollViewDelegate

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self hideKeyboard];
}

我希望它有所帮助! =)

答案 23 :(得分:0)

如果你想在按下返回键的同时关闭键盘,你只需在textField中添加以下代码即可返回方法,即:

- (BOOL)textFieldShouldReturn:(UITextField *)atextField
{
   [textField resignFirstresponder];
}

某些文本字段可能有一个选择器视图或其他一些作为子视图,因此在这种情况下上述方法不起作用,因此在这种情况下我们需要使用UITapGestureRecognizer类,即将以下代码片段添加到viewDidLoad方法,即:

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

    [self.view addGestureRecognizer:tap];

现在只需将resign responder添加到选择器方法,即:

-(void)dismissKeyboard 
{
    [textField resignFirstResponder];
}

希望它有所帮助,谢谢:)

答案 24 :(得分:0)

UITableView有一个方便的backgroundView属性,我在不破坏单元格选择的情况下实现了这种行为,如下面的Swift所示:

let tableBackTapRecognizer = UITapGestureRecognizer(target: self, action: #selector(hideKeyboard))
tableView.backgroundView = UIView()
tableView.backgroundView?.addGestureRecognizer(tableBackTapRecognizer)

答案 25 :(得分:0)

如果你愿意为你的表视图创建子类(呃!),那么something like this可能会有效:

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

   BOOL backgroundTouched = YES;

   for (UITouch *touch in touches) {
      CGPoint location = [touch locationInView:self];
      for (UITableViewCell *cell in self.visibleCells) {
         if (CGRectContainsPoint(cell.frame, location)) {
            backgroundTouched = NO;
            break;
         }
      }
   }

   if (backgroundTouched) {
      for (UITableViewCell *cell in self.visibleCells) {
         // This presumes the first subview is the text field you want to resign.
         [[cell.contentView.subviews objectAtIndex:0] resignFirstResponder];
      }
   }

   [super touchesBegan:touches withEvent:event];
}

答案 26 :(得分:0)

尝试一下:

viewDidLoad(){

    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))

    tableView.addGestureRecognizer(tap)

}
//Calls this function when the tap is recognized.
@objc func dismissKeyboard() {

    //Causes the view (or one of its embedded text fields) to resign the first responder status.
    view.endEditing(true)

}

答案 27 :(得分:0)

迅速4 / 4.2 / 5

您也可以在轻按单元格时关闭键盘-在进行其他操作之前。

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    view.endEditing(true)
    // Do something here
    }

答案 28 :(得分:0)

为什么要创建一个充满文本字段的表?您应该为包含文本字段的每一行使用详细视图。 推送详细视图时,请确保调用“[myTextField becomeFirstResponder]”,以便用户只需单击一下表格列表即可开始编辑。