Resizing contentView of UITextView when keyboard appears?

I have a tableView top of the keyboard. After the last line, there is a huge ’empty’ space. I guess the contextSize does not refreshed.

In storyboard I have set up a bottom constraint between for tableView. And I set up notification observer opening keyboard, but something missing. How can I prevent this empty space?

NSNotificationCenter.defaultCenter().addObserverForName(UIKeyboardDidShowNotification, object: nil, queue:NSOperationQueue.mainQueue(), usingBlock: {(notification: NSNotification?) -> () in

    let keyboardSize = (notification!.userInfo![UIKeyboardFrameBeginUserInfoKey] as? NSValue)!.CGRectValue().size
    self.bottomConstraint.constant = keyboardSize.height-44
    self.view.layoutIfNeeded()

    if let messageTableViewController2 = messageTableViewController {

        /*messageTableViewController2.tableView.beginUpdates() <- this is what i have already tried
        messageTableViewController2.tableView.layoutIfNeeded()
        messageTableViewController2.tableView.endUpdates()

        var frame = messageTableViewController2.tableView.frame
        frame.size.height = messageTableViewController2.tableView.frame.size.height
        messageTableViewController2.tableView.frame = frame*/
    }
})

NSNotificationCenter.defaultCenter().addObserverForName(UIKeyboardDidHideNotification, object: nil, queue:NSOperationQueue.mainQueue(), usingBlock: {(notification: NSNotification?) -> () in

    let keyboardSize = (notification!.userInfo![UIKeyboardFrameBeginUserInfoKey] as? NSValue)!.CGRectValue().size
    self.bottomConstraint.constant = 0
    self.view.layoutIfNeeded()
})

deinit {

    NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardDidShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardDidHideNotification, object: nil)
}

enter image description here


Source: ios

Leave a Reply