Step 1: Create a new controller
Select File → New → File → Cocoa Touch Class in Xcode
Create a new LoginViewController inherits from UIViewController
Step 2: Create two UITextFields
passwordInput: UITextField // Password input box
accountInput: UITextField // Account input box
Step 3: Add keyboard KVO
Add the following two lines of code in the viewDidLoad method
//A notification will be sent to the system when the keyboard pops up.//A listener needs to be registered at this time to respond to notification().addObserver(self, selector: #selector((_:)), name:UIKeyboardWillShowNotification, object: nil) //A notification will be sent to the system when the keyboard is closed.//Another listener needs to be registered to respond to the notification().addObserver(self, selector: #selector((_:)), name:UIKeyboardWillHideNotification, object: nil)
Add global control parameters
Because when switching between two or more textfields in succession, only the UIKeyboardWillShowNotification keyboard display notification will be sent, and the UIKeyboardWillHideNotification keyboard hidden notification will not be sent. This requires a global parameter to control the interface to only move up when the input box is clicked the first time. The parameter becomes false, and the interface will no longer change when the cursor moves to another input box. When the keyboard is turned off, the interface moves down and restores this parameter to the default value.
Declare the variable on the first line of the class:
var keyBoardNeedLayout: Bool = true
Add two methods to pop up and hide the keyboard accordingly
The keyboard pops up to respond
func keyboardWillShow(notification: NSNotification) { print("show") if let userInfo = , value = userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue, duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? Double, curve = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? UInt { let frame = () let intersection = CGRectIntersection(frame, ) let deltaY = CGRectGetHeight(intersection) if keyBoardNeedLayout { (duration, delay: 0.0, options: UIViewAnimationOptions(rawValue: curve), animations: { _ in = CGRectMake(0,-deltaY,,) = false () }, completion: nil) } } }
Keyboard hidden response
func keyboardWillHide(notification: NSNotification) { print("hide") if let userInfo = , value = userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue, duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? Double, curve = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? UInt { let frame = () let intersection = CGRectIntersection(frame, ) let deltaY = CGRectGetHeight(intersection) (duration, delay: 0.0, options: UIViewAnimationOptions(rawValue: curve), animations: { _ in = CGRectMake(0,deltaY,,) = true () }, completion: nil) } }
Going further
If the input box is bottomed, the displacement of y can be used -deltaY
= CGRectMake(0,-deltaY,,)
However, if the input box is in an upper position, it may cause a certain input box to move out of the interface vision. At this time, you can write the displacement as deltaY/2 or deltaY/4, etc., and try it yourself.
The above is the editor’s introduction to Swift to make the input box pop up with the keyboard to avoid the input method blocking the input box. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!