I have this CommentViewController
. It's embedded in a Container View. In this CommentViewController
, there's a UITableView
to display comments and UIView
that contains a UITextField
and a UIButton
. The UIView
that's containing those 2 objects is fixed via autolayout in the bottom, right and left to the Safe Area and top to the UITableView
.
Now, when the user taps the UITextField
, the whole view (with the button and the textfield inside) should be lifted above the keyboard. I'm trying to animated the bottom constraint of the view, but it's not working.
This is the code:
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
@objc func keyboardWillShow(_ notification: NSNotification) {
let keyboardFrame = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as AnyObject).cgRectValue
UIView.animate(withDuration: 0.3) {
self.textFieldViewBottomConstraint.constant = keyboardFrame!.height
}
}
This is storyboard:
This is the result after you tap the text field.
PS: when I tap on UITextField
, the console shows this message:
2018-06-04 14:11:52.471848+0300 AppName[91846:8829073] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /Users/d/Library/Developer/CoreSimulator/Devices/C89347A2-1598-4F31-BBAC-1F98F970A248/data/Containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
2018-06-04 14:11:52.472588+0300 Shippers[91846:8829073] [MC] Reading from private effective user settings.