Swift View Float On Keyboard Show

[Solved] Swift View Float On Keyboard Show | Swift - Code Explorer | yomemimo.com
Question : swift view float on keyboard show

Answered by : sarwar

//swift 4.2 and above "5.3 tested"
override func viewDidLoad() { super.viewDidLoad() NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
}
@objc func keyboardWillShow(notification: NSNotification) { if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue { if self.view.frame.origin.y == 0 { self.view.frame.origin.y -= keyboardSize.height //we can also change constraints runtime } }
}
@objc func keyboardWillHide(notification: NSNotification) { if self.view.frame.origin.y != 0 { self.view.frame.origin.y = 0 //this area will set old constraints again }
}

Source : https://stackoverflow.com/a/31124676 | Last Update : Fri, 19 Feb 21

Answers related to swift view float on keyboard show

Code Explorer Popular Question For Swift