开发者

How can I dismiss the keyboard if a user taps off the on-screen keyboard?

I want to be able to dismiss the iPhone keyboard when the user taps anywhere outside of the keyboard. How can I go about doing 开发者_运维知识库this? I know I need to dismiss the responder, but need to know how to implement it when a user taps out of the keyboard space.


You'll need to add an UITapGestureRecogniser and assign it to the view, and then call resign first responder on the textfield on it's selector.

The code:

In viewDidLoad

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

[self.view addGestureRecognizer:tap];

In dismissKeyboard:

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

(Where aTextField is the textfield that is responsible for the keyboard)

OPTION 2

If you can't afford to add a gestureRecognizer then you can try this

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch * touch = [touches anyObject];
    if(touch.phase == UITouchPhaseBegan) {
        [aTextField resignFirstResponder];
    }
}


The simplest solution I have used is this:

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

      [self.view endEditing:YES];

}

The endEditing command can be used on any view that contains your textfield as a subview. The other advantage of this method is that you don't need to know which textfield triggered the keyboard. So even if you have a multiple textfields, just add this line to the superview.

Based on the Apple documentation, I think this method exists specifically to solve this problem.


Add a tapGesture Recognizer but make sure cancelsTouchesInView = NO

UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(closeTextInput)];
tapGesture.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapGesture];
[tapGesture release];


You'll need to add an UITapGestureRecogniser and assign it to the view, and then call resign first responder on the textfield on it's selector.

The code:

In viewDidLoad

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

[self.view addGestureRecognizer:tap];

In dismissKeyboard:

-(void)dismissKeyboard {
       [self.view endEditing:true];
}


You need to add a transparent UIVIew as a subview below the keyboard and handle touches there, to dismiss the keyboard. Below code is for your reference.

UITapGestureRecognizer* gesture = [[UITapGestureRecognizer alloc]   initWithTarget:self action:@selector(overlayTouched:)]; 
gesture.delegate = self;
[(UITapGestureRecognizer *)gesture setNumberOfTouchesRequired:1];

UIView* trans = [[UIView alloc] initWithFrame:[[delegate view] bounds]];
[trans setOpaque:NO];
[trans setAutoresizingMask:UIViewAutoresizingFlexibleWidth |    UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin];
[trans setAlpha:0.3];
[trans setUserInteractionEnabled:YES];
trans.multipleTouchEnabled = YES;
[trans addGestureRecognizer:gesture];
[trans setBackgroundColor:[UIColor blackColor]];
[trans setTag:BLACK_SCREEN_VIEW];


This is a Swift 4 solution:

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

 self.view.addGestureRecognizer(tap)

And the dismissKeyboard

@objc func dismissKeyboard() {
    self.view.endEditing(true)
}


Other way to do is simple:

Makes your UIView as UIControl in custom class in the interface builder, then you can attach an IBAction method in Touch up inside event of your UIView : UIControl, then you put [yourTextField resignFirstResponder] inside the IBAction method, like this:

- (IBAction) hideKeyboard: (id) sender
{
    // If you have more than one textfield to dismiss, of course only can be active 1, but here you can't know who is it, because sender will be the UIView : UIControl
    [alias resignFirstResponder];
    [password resignFirstResponder];
}

Then, you have other option and it's to put in your textfield the return key of the keyboard as Done (It can be any of those you want, but Done it's good for this, because return means to do an action with the form) in the interface builder, so you can press Done and hide the keyboard, but in that case you have to attach the previous IBAction method to the Did end on exit event.

And in this way the keyboard will hide touching outside or touching Done from the keyboard.

If you want to improve the code, if only will hide the keyboard touching Done from the keyboard the method should be:

// Attach all textFields here on Did end on exit event, will not work if touch outside the keyboard
- (IBAction) hideKeyboard: (id) sender
{
      [sender resignFirstResponder];
}


If you are on a UITableViewController or have a UITableView that the user will be interacting with, in your ViewDidLoad you can simply do:

tableView.KeyboardDismissMode = UIScrollViewKeyboardDismissMode.OnDrag;

Note: this is Xamarin.iOS syntax.


If you don't want to add a gesture recognizer you can also use the touchesBegin method

Code in Swift 4

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { view.endEditing(true) }


You don't even need a gesture recogniser. Views can detect touches without gesture recognisers. All you need is literally this in your view controller....

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

      [self.view endEditing:YES];

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜