I have a UITextField
. It works normally, meaning that when pressed, it fires EditingDidBegin
method etc. The problem is that I cannot deselect it, meaning that the keyboard will be always visible, covering important controls. I thought that default behavior would be deselecting/hiding keyboard when user clicks somewhere different from the UITextField
, for example on the empty space around it, but it's not - the UITextField
is still there. One option to achieve this behavior would be adding touch listener to everything other than UITextFields
and perform manual unfocusing when it fires, but that would be a terrible solution for obvious reasons.
Question is, how do I unfocus
UITextFields
when user presses other controls/"blank space"?
Proposed options don't work for Xamarin.iOS which is the one I need.
If you're supporting only iOS 15 and later, you can activate and dismiss the keyboard for a text field by focusing and unfocusing it. In its simplest form, this is done using the @FocusState property wrapper and the focusable() modifier – the first stores a Boolean that tracks whether the second is currently focused.
Android devices have a solution; press the physical back button (provided on some mobile phones) or the soft key back button, and it closes the keyboard.
Via Tap Gesture This is the quickest way to implement keyboard dismissal. Just set a Tap gesture on the main View and hook that gesture with a function which calls view. endEditing .
I always add this to my ViewController
override func viewDidLoad()
{
super.viewDidLoad()
// other stuff
// but to stop editing when the user taps anywhere on the view, add this gesture recogniser
let tapGestureBackground = UITapGestureRecognizer(target: self, action: #selector(self.backgroundTapped(_:)))
self.view.addGestureRecognizer(tapGestureBackground)
}
func backgroundTapped(_ sender: UITapGestureRecognizer)
{
self.endEditing(true)
}
SWIFT
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
Objective C
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
}
It will resign your textField or textView by tapping outside of it.
Note: It will not affect on scrollView
Scroll View
Objective C
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
tapGesture.cancelsTouchesInView = NO;
[yourScrollView/TableView addGestureRecognizer:tapGesture];
-(void)hideKeyboard {
[textFieldInYourScrollView/TableView resignFirstResponder];
}
SWIFT
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(hideKeyboard))
tapGesture.cancelsTouchesInView = false
scrollView.addGestureRecognizer(tapGesture)
@objc func hideKeyboard() {
self.view.endEditing(true)
}
Hide Keyboard on Scrolling
scrollView.keyboardDismissMode = .onDrag
tableView.keyboardDismissMode = .onDrag
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With