Search This Blog

Showing posts with label Keyboard. Show all posts
Showing posts with label Keyboard. Show all posts

Friday, February 2, 2018

iOS Swift. Check if keyboard is visible

How we can get is keyboard is shown or hidden currently in our app?

Keyboard is shown

That is it. Simple solution, we just need to add observer for keyboard notification.
override func viewDidLoad() {
    super.viewDidLoad()
        
    NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardShow),
                                           name: Notification.Name.UIKeyboardWillShow, object: nil)       
}
    
@objc func handleKeyboardShow(notification: Notification) {
    print("Keyboard will show")
}
This observer only works for event when keyboard will be shown, there is another observer for event when keyboard will be hidden.

override func viewDidLoad() {
    super.viewDidLoad()
        
    NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardShow),
                                           name: Notification.Name.UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardHide),
                                           name: Notification.Name.UIKeyboardWillHide, object: nil)
}
    
@objc func handleKeyboardShow(notification: Notification) {
    print("Keyboard will show")
}
    
@objc func handleKeyboardHide(notification: Notification) {
    print("Keyboard will hide")
}
But there is a lot more thing beside this. And I want to tell you about some of them.

Monday, February 27, 2017

3 ways to hide keyboard in ios

There is one of most popular issues in ios development. How to hide keyboard on touch outside or when Return button is tapped in keyboard. There are different ways how to do it.


1. override func touchesBegan


The easiest one. Just override one method of UIView - touchesBegan and invoke method endEditing of view.

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }
   
}

2. Using UITapGestureRecognizer


Create gesture recognizer for tap gestures and add it to the view. On gesture handler hide the keyboard.

class ViewController: UIViewController {
    
    @IBOutlet weak var textField: UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        let tapRecognizer = UITapGestureRecognizer(target: self, 
            action: #selector(ViewController.dismissKeyboard))
        self.view.addGestureRecognizer(tapRecognizer)
    }
    
    func dismissKeyboard() {
        self.textField.resignFirstResponder()
    }
    
}

3. Using UITextFieldDelegate

There is how to hide keyboard when Return button of Keyboard is tapped. We need to be UITextFieldDelegate, make self as delegate and implement textFieldShouldReturn method in which we will hide keyboard.

class ViewController: UIViewController, UITextFieldDelegate {
    
    @IBOutlet weak var textField: UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.textField.delegate = self
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        self.dismissKeyboard()
        return false
    }
   
}