I'm trying to hide the keyboard after a touch anywhere else on the screen. The code I'm using is based on this answer here.
IBOutlet UITextView *myTextView;
And the method:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if ([myTextView isFirstResponder] && [touch view] != myTextView) {
[myTextView resignFirstResponder];
}
[super touchesBegan:touches withEvent:event];
}
What I don't understand is how I should link my UITextField to the touchesBegan
method. Which sent event do I need to use? Also, shouldn't the method be an IBAction, because right now I can't connect my UITextField to it.
I also gave this code a try but that one was breaking my navigation buttons (even with the solution mentioned in the comments)
Try the quick and dirty way:
Take a button and link it to an action method(lets call it Background). Stretch the button so it covers the whole view. Adjust the layers of the views so only things the user interacts by touch are on top of the button. Change the type of button to custom, this make the button invisible. Dismiss the firstResponder in the method Background.
What I do, is change the overall UIView class to UIControl.
This gives you a touchDown event you can link up to a method to resignFirstResponder.
The UIControl still gives you all the functionality of a UIView.
Objective C:
Swift:
This is the best way I have found and it is very simple.
Because you don't. You have to override this method on the view of which the text field is a subview.