Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to move inputView above keyboard when scrolling the table

I have a view controller in which it has table view and textView below the table

When I tap on the textView the keyboard appears and when I tap outside the textView the keyboard disappar That works fine but I have one problem which is when I Drag(scroll Down) the table view the textView doesn't move down with the keyboard and I see black background behind the keyboard as in the below image

keyboard disappear with textview

How to solve this problem in Swift

Update: This is my current code that observe the keyboardFrameChanges

func keyboardFrameChanged(notification: NSNotification) {

    let dict = NSDictionary(dictionary: notification.userInfo!)
    let keyboardValue = dict.objectForKey(UIKeyboardFrameEndUserInfoKey) as! NSValue
    let bottomDistance = mainScreenSize().height - keyboardValue.CGRectValue().origin.y
     let duration = Double(dict.objectForKey(UIKeyboardAnimationDurationUserInfoKey) as! NSNumber)

    UIView.animateWithDuration(duration, animations: {
        self.inputViewConstraint!.constant = -bottomDistance
        self.view.layoutIfNeeded()
        }, completion: {
            (value: Bool) in
            self.chatTableView.scrollToBottom(animation: true)
    })

}

Update 2: I found a solution by changing the keyboardDismissMode from Interactive to OnDrag

chatTableView.keyboardDismissMode = UIScrollViewKeyboardDismissMode.OnDrag

This will move the keyboard and the textView immediatly to down once it observe any drag movement in the table , But how to do it in the Interactive mode like in the whatsapp chat view

like image 496
Musa almatri Avatar asked Aug 29 '16 15:08

Musa almatri


People also ask

Which component is used to move UI up when keyboard appears?

This component will automatically adjust its height, position, or bottom padding based on the keyboard height to remain visible while the virtual keyboard is displayed.


2 Answers

  1. set you scroll view (UITableView or UICollectionView) frame equal to you UIViewController frame
  2. set .interactive to keyboardDismissMode of you scroll view
  3. override canBecomeFirstResponder and return true
  4. setup input container with UITextField or UITextView in screen bottom
  5. override inputAccessoryView and return input container
  6. enjoy

See my simple code here on github

If you implement your own bottom constraint for input view

And change its constant on the keyboard frame notifications. Using these lines of code, the error of predictive bar layout at the end of the animation will be fixed.

override var inputAccessoryView: UIView? {
    return UIView()
}

override var canBecomeFirstResponder: Bool {
    return true
}
like image 148
Roman Filippov Avatar answered Oct 29 '22 20:10

Roman Filippov


Finding the answer for this took forever but this is the correct way of implementing this feature with the interactive state enabled.

In your view controller that has the tableview or collection view displaying chat bubbles override inputAccessoryView, then return the view that contains the uitextView or uitextField. and override canBecomeFirstResponder then return true.

override var inputAccessoryView: UIView? {
    get {
        self.inputBar.frame.size.height = self.barHeight // 50.0
        self.inputBar.clipsToBounds = true
        return self.inputBar
    }
}
override var canBecomeFirstResponder: Bool{
    return true
}
like image 27
Woody Jean-louis Avatar answered Oct 29 '22 19:10

Woody Jean-louis