Swift makes the input box pop up along with the keyboard to prevent the input method from blocking the input box

  • 2020-05-15 02:14:48
  • OfStack

Step 1: create a new Controller

Select File → New → File → Cocoa Touch Class in Xcode

New LoginViewController inherits from UIViewController

Step 2: create two UITextField

passwordInput: UITextField // password entry box
accountInput: UITextField // account entry box

Step 3: add the keyboard KVO

Add the following two lines of code to the viewDidLoad method


// It is sent to the system when the keyboard is played 1 A notice, 
// Registration is required at this time 1 Three listeners respond to the notification 
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(LoginViewController.keyboardWillShow(_:)), name:UIKeyboardWillShowNotification, object: nil)
// It is sent to the system when the keyboard is retracted 1 A notice, 
// Registration is required at this time 1 Three listeners respond to the notification 
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(LoginViewController.keyboardWillHide(_:)), name:UIKeyboardWillHideNotification, object: nil)

Add global control parameters

Because when switching between two or more textfield continuously, only UIKeyboardWillShowNotification keyboard display notifications will be sent, but UIKeyboardWillHideNotification keyboard hidden notifications will not be sent. This requires a global parameter to control the keyboard to move up the interface only when the input box is clicked on the first time. This parameter becomes false, and the interface will not change when the cursor moves to another input box. When the keyboard is turned off, the screen moves down and this parameter is restored to its default value.

Declare the variable on line 1 of the class:


var keyBoardNeedLayout: Bool = true

Add two methods corresponding to keyboard up and keyboard hide, respectively

The keyboard bounced in response


func keyboardWillShow(notification: NSNotification) {
print("show")
if let userInfo = notification.userInfo,
value = userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue,
duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? Double,
curve = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? UInt {
let frame = value.CGRectValue()
let intersection = CGRectIntersection(frame, self.view.frame)
let deltaY = CGRectGetHeight(intersection)
if keyBoardNeedLayout {
UIView.animateWithDuration(duration, delay: 0.0,
options: UIViewAnimationOptions(rawValue: curve),
animations: { _ in
self.view.frame = CGRectMake(0,-deltaY,self.view.bounds.width,self.view.bounds.height)
self.keyBoardNeedLayout = false
self.view.layoutIfNeeded()
}, completion: nil)
}
}
}

Keyboard hidden response


func keyboardWillHide(notification: NSNotification) {
print("hide")
if let userInfo = notification.userInfo,
value = userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue,
duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? Double,
curve = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? UInt {
let frame = value.CGRectValue()
let intersection = CGRectIntersection(frame, self.view.frame)
let deltaY = CGRectGetHeight(intersection)
UIView.animateWithDuration(duration, delay: 0.0,
options: UIViewAnimationOptions(rawValue: curve),
animations: { _ in
self.view.frame = CGRectMake(0,deltaY,self.view.bounds.width,self.view.bounds.height)
self.keyBoardNeedLayout = true
self.view.layoutIfNeeded()
}, completion: nil)
}
}

Further more

If the input box sucks the bottom, the displacement of y can be -deltaY


self.view.frame = CGRectMake(0,-deltaY,self.view.bounds.width,self.view.bounds.height)

However, if the input box is in the upper position, it may cause one of the input boxes to move out of the interface horizon. In this case, you can write the displacement as deltaY/2 or deltaY/4, etc. Try it yourself.


Related articles: