Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I unfocus UITextFields when user presses other controls/blank space?

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.

like image 562
nicks Avatar asked Jan 28 '17 06:01

nicks


People also ask

How do I dismiss the keyboard for a textfield?

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.

How do you dismiss a keyboard?

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.

How do I close open keyboard forcefully in Swift?

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 .


2 Answers

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)   
}
like image 107
Russell Avatar answered Oct 02 '22 21:10

Russell


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
like image 45
Harshal Valanda Avatar answered Oct 02 '22 23:10

Harshal Valanda