OK, so I am trying to implement an add comment box in my view. Here is the layout:
I have a UITabBarController
containing a UINavigationController
where my UIViewController
is pushed to. The View Layout consists of a UITableView
and a custom UIView
that contains a UITextView
and a UIButton
.
I am monitoring for keyboard notifications, this is the code I have:
override func viewDidAppear(animated: Bool) {
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(keyboardWillAppear), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(keyboardWillDisappear), name: UIKeyboardWillHideNotification, object: nil)
}
override func viewDidDisappear(animated: Bool) {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
func keyboardWillAppear(notification: NSNotification){
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
var frame = inputPanel.frame
frame.origin.y = self.view.frame.height - (keyboardSize.height + inputPanel.frame.size.height)
inputPanel.frame = frame
}
}
func keyboardWillDisappear(notification: NSNotification){
var frame = inputPanel.frame
frame.origin.y = self.view.frame.height - inputPanel.frame.size.height
inputPanel.frame = frame
}
When the keyboard displays, I have set breakpoints and see that the y coordinate is updated, but my view stays where it is, hidden behind the keyboard.
A couple of notes. I am using IQKeyboardManager
and have disabled distance handling for this class. I am also using PureLayout
for laying out the controls inside of the custom UIView
. I am not setting any constraints for the custom UIView
in the view controller. The positions of the UITableView
and the custom UIView
are set in the viewWillLayoutSubview
method as below:
override func viewWillLayoutSubviews() {
let y = self.view.frame.height - 44
self.tableView.frame = CGRectMake(0, 0, self.view.frame.width, y)
self.inputPanel.frame = CGRectMake(0, y, self.view.frame.width, 44)
}
On another note, this is working perfectly in another view I have that is not a child of a UINavigationController
.
Screen shots of what is happening:
The view with the input view:
When the keyboard is shown:
Any help is greatly appreciated.