【问题标题】:Not been able to dismiss keyboard on tap无法在点击时关闭键盘
【发布时间】:2025-12-29 15:30:06
【问题描述】:

我正在尝试在视图中的任意位置关闭点击键盘。这是我的代码

- (void)registerForNotifcationOfKeyboard
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification object:nil];
}

// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    CGRect bkgndRect = activeField.superview.frame;
    bkgndRect.size.height += kbSize.height;
    [activeField.superview setFrame:bkgndRect];
    [scrollView setContentOffset:CGPointMake(0.0,kbSize.height/2 - activeField.frame.origin.y) animated:YES];
}

- (void) textFieldDidBeginEditing:(UITextField *)textField
{
    activeField = textField;
}

- (void) textFieldDidEndEditing:(UITextField *)textField
{
    activeField = nil;
}

-(BOOL) disablesAutomaticKeyboardDismissal
{
    return NO;
}

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    scrollView.contentInset = contentInsets;
    scrollView.scrollIndicatorInsets = contentInsets;

}


- (void)viewDidLoad
{
    [self registerForNotifcationOfKeyboard];
     self.progressBar.hidden = YES;

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


    [super viewDidLoad];
}

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

当我在任意位置按下 tap 时,此函数 dismissKeyboard 会被调用,但它不会关闭键盘。

如果有人有任何想法,敬请期待

最好的问候

【问题讨论】:

  • 您是否将委托正确连接到 xib 文件中的文件所有者?或者您可以使用 activeField.delegate=self;在 viewDidLoad 中。

标签: iphone objective-c cocoa-touch ios5


【解决方案1】:

总是适合我:

//if `dismissKeyboard` is located in your `UIViewController`'s sublass: 
-(void) dismissKeyboard
{
   [self.view endEditing:YES];
}

来自 UIView 类参考:

 This method looks at the current view and its subview hierarchy for the text field that is currently the first responder.   
If it finds one, it asks that text field to resign as first responder. If the force parameter is set to **YES**, the text field is never even asked; it is **forced to resign**.

【讨论】:

    【解决方案2】:

    我完全同意@Lukasz:使用视图的 endEditing 属性来关闭键盘

    -(void) dismissKeyboard
    {
      [self.view endEditing:YES]; //this will dissmiss keyboard;
    }
    

    【讨论】:

      【解决方案3】:

      试试下面的代码。

      在此之前在 .h 文件中使用 UITextFieldDelegate。

      在 .m 文件中

       textfieldname.delegate=self;
      
      
      
       - (BOOL)textFieldShouldReturn:(UITextField *)textField
      {
      
      
          [textfieldname resignFirstResponder];
      
          return YES;
      
      }
      

      上面的代码将在按下返回键时关闭键盘。在你的函数中使用下面的代码来关闭键盘。

      [textfieldname resignFirstResponder];
      

      【讨论】:

      • 他想用他的方法dismissKeyboard 关闭他的键盘,而不是在点击返回时。
      • -(void)dismissKeyboard { [textfieldname resignFirstResponder]; }
      • 请看他代码的最后几行,他已经这样做了。
      • 我猜他可能没有将代理连接到 .xib 文件中的文件所有者。
      • 他已经为UITapGestureRecognizer 编写了该代码,将委托设置为UITextField 无关紧要。
      【解决方案4】:

      如果您想通过按视图内的任意位置来关闭键盘,那么您可以做 2 件事:

      1:- 有一个 UIButton,它具有 [UIColor clearColor] 并且等于视图的大小,并且在您关闭键盘的按钮的 IBAction 中。这不是一个好的做法,尽管它似乎有效。

      2:- 转到身份检查器并将视图的类更改为 UIControl 类,然后将 IBAction 添加到您的类以关闭您的键盘。

      我希望这会有所帮助。 干杯!!

      【讨论】:

      • 并且@lukasz 以这种方式关闭键盘是正确的。