iOS - Dismiss keyboard when touching outside of UI

2019-01-01 01:37发布

I'm wondering how to make the keyboard disappear when the user touches outside of a UITextField.

30条回答
春风洒进眼中
2楼-- · 2019-01-01 01:42

If I got you right you want to resign keyboard wile tapping on outSide of textfield but you don't have reference of your textfield.

Try this;

  • Take global textField, lets call it reftextField
  • Now in textFieldDidBeginEditing set referenced text field to

    - (void) textFieldDidBeginEditing:(UITextField *)textField{
        reftextField = textField;
    }
    
  • Now you can happily use on any button clock, (adding a transparent button on begin editing recomended)

    - (void)dismissKeyboard {
          [reftextField resignFirstResponder];
    }
    
  • Or for resigning done button try this.

    //for resigning on done button    
    - (BOOL) textFieldShouldReturn:(UITextField *)textField{
        [textField resignFirstResponder];
        return YES;
    }
    
查看更多
唯独是你
3楼-- · 2019-01-01 01:42

You can create category for the UiView and override the touchesBegan meathod as follows.

It is working fine for me.And it is centralize solution for this problem.

#import "UIView+Keyboard.h"
@implementation UIView(Keyboard)

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.window endEditing:true];
    [super touchesBegan:touches withEvent:event];
}
@end
查看更多
还给你的自由
4楼-- · 2019-01-01 01:43

This is a good generic solution:

Objective-C:

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

Swift:

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    self.view.endEditing(true)
}

Based on @icodebuster solution: https://stackoverflow.com/a/18756253/417652

查看更多
步步皆殇っ
5楼-- · 2019-01-01 01:43
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    if let touch = touches.first{
     view.endEditing(true)

     }
}
查看更多
低头抚发
6楼-- · 2019-01-01 01:44

It is better to make your UIView an instance of UIControl (in interface builder) and then connect their TouchUpInside event to dismissKeyboard method. This IBAction method will look like:

- (IBAction)dismissKeyboard:(id)sender {
    [aTextBox resignFirstResponder];
}
查看更多
查无此人
7楼-- · 2019-01-01 01:44

I used Barry example for my new development. It worked great! but i had to include a slightly change, required to dismiss the keyboard only for the textfield being edited.

So, I added to Barry example the following:

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

Also, I changed hideKeyboard method as follows:

- (IBAction)hideKeyboard:(id)sender
{
    // Just call resignFirstResponder on all UITextFields and UITextViews in this VC
    // Why? Because it works and checking which one was last active gets messy.
    //UITextField * tf = (UITextField *) sender;
    [_textBeingEdited resignFirstResponder];
}
查看更多
登录 后发表回答