How to Force My Keyboard to Be Up on My Program's Start in Swift

How can I force my keyboard to be up on my program's start in Swift?

In your viewDidAppear method, add the following:

txtField1.becomeFirstResponder()

For more information about UIResponder's becomeFirstResponder(), check the documentation.

How to make iOS keyboard appear/stay up during entire app lifecycle?

I think that you can't do this from storyboard side, but from code side, assuming that you name your textfield "textField", you can do self.textField.becomeFirstResponder() on your viewDidLoad() in order to make the keyboard appear when you enter on this screen. This should keep the keyboard on screen unless you do resignFirstResponder() elsewhere.

Causing cancel button and software keyboard to appear automatically in viewDidLoad()

For the cancel button, you can make it appear automatically using searchBar.showsCancelButton = true. However, this will not respond to touches until the keyboard appears.

Calling .becomeFirstResponder on the searchBar will only work after the searchController is finished loading. Please reference the following article: Cannot set searchBar as firstResponder.

Calling searchBar.becomeFirstResponder() in viewDidAppear(), and even forcing it onto the main thread using DispatchQueue.main.async{} (as some people suggest) did not work. The following code snippet, called from viewDidLoad(), is my current working solution:

    DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(700), execute: {
self.searchController?.searchBar.becomeFirstResponder()
})

I may adjust the delay to make it longer, to ensure that it works on all devices. This works on an iPhoneXR.

Swift - Get keyboard input as the user is typing

You need to register as observer for notifications to see when the keyboard appears and disappears. Then you would move your view up on show, or restore it to original on hide.

My implementation moves the whole view up by keyboard height, but if you want you can just move UITextField up.

You can take a look at this tutorial, or see my implementation in the answer:

http://www.ioscreator.com/tutorials/move-view-behind-keyboard-ios8-swift

Add observers in viewDidAppear

override func viewWillAppear(animated:Bool) {
super.viewWillAppear(animated)

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
}

You then need the methods to move the view in keyboardWillShow and keyboardWillHide new methods.

@objc func keyboardWillShow(_ notification: NSNotification) {
let info = notification.userInfo!
let keyboardSize = (info[UIResponder.keyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue

let keyboardHeight:CGFloat = keyboardSize.height

//let animationDuration:CGFloat = info[UIResponder.keyboardAnimationDurationUserInfoKey] as! CGFloat

UIView.animate(withDuration: 0.25, delay: 0, options: .curveEaseInOut, animations: {
self.view.transform = CGAffineTransform(translationX: 0, y: -keyboardHeight)
}, completion: nil)

}

And to move the view to original state:

@objc func keyboardWillHide(_ notification: NSNotification) {
UIView.animate(withDuration: 0.25, delay: 0, options: .curveEaseInOut, animations: {
self.view.transform = CGAffineTransform.identity
}, completion: nil)
}

And at the end remove the observer in viewDidDisappear

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self)
}

Close iOS Keyboard by touching anywhere using Swift

override func viewDidLoad() {
super.viewDidLoad()

//Looks for single or multiple taps.
let tap = UITapGestureRecognizer(target: self, action: #selector(UIInputViewController.dismissKeyboard))

//Uncomment the line below if you want the tap not not interfere and cancel other interactions.
//tap.cancelsTouchesInView = false

view.addGestureRecognizer(tap)
}

//Calls this function when the tap is recognized.
@objc func dismissKeyboard() {
//Causes the view (or one of its embedded text fields) to resign the first responder status.
view.endEditing(true)
}

Here is another way to do this task if you are going to use this functionality in multiple UIViewControllers:

// Put this piece of code anywhere you like
extension UIViewController {
func hideKeyboardWhenTappedAround() {
let tap = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
}

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

Now in every UIViewController, all you have to do is call this function:

override func viewDidLoad() {
super.viewDidLoad()
self.hideKeyboardWhenTappedAround()
}

This function is included as a standard function in my repo which contains a lot of useful Swift Extensions like this one, check it out: https://github.com/goktugyil/EZSwiftExtensions

It doesn't look like the return key is working on my method

Please make sure that the above function is called. If it is not called, then you need to set the delegate of your textField. You can do that as follows:

If txtViewQuestionStatus this is your UITextField, in your viewDidLoad() method, add the following line:

txtViewQuestionStatus.delegate = self

If there is error in the above line showing delegate is not set, You need to add UITextFieldDelegate in your class. Like :

class ViewController : UIViewController  <UITextFieldDelegate>


Related Topics



Leave a reply



Submit