Last active
February 20, 2020 18:45
-
-
Save ergunkocak/9b1989131a6f629466ede3acf6eb0ec6 to your computer and use it in GitHub Desktop.
Update button constraint by keyboard open and close
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override func viewDidLoad() { | |
super.viewDidLoad() | |
NotificationCenter.default.addObserver(self, selector: #selector(keyboardNotification(notification:)), name: UIResponder.keyboardWillChangeFrameNotification, object: nil) | |
} | |
@objc func keyboardNotification(notification: NSNotification) { | |
if let userInfo = notification.userInfo { | |
let endFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue | |
let endFrameY = endFrame?.origin.y ?? 0 | |
let duration:TimeInterval = (userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0 | |
let animationCurveRawNSN = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? NSNumber | |
let animationCurveRaw = animationCurveRawNSN?.uintValue ?? UIView.AnimationOptions.curveEaseInOut.rawValue | |
let animationCurve:UIView.AnimationOptions = UIView.AnimationOptions(rawValue: animationCurveRaw) | |
if endFrameY >= UIScreen.main.bounds.size.height { | |
keyboardHeightLayoutConstraint?.constant = 8 | |
} else { | |
keyboardHeightLayoutConstraint?.constant = (endFrame?.size.height ?? 0.0) + 8 | |
} | |
UIView.animate(withDuration: duration, | |
delay: TimeInterval(0), | |
options: animationCurve, | |
animations: { self.view.layoutIfNeeded() }, | |
completion: nil) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment