InputAccessoryView move table view up and down with keyboard show/hide interactively

Viewed 278

I've been checking out various tuts on how to move table view up/down with keyboard interactively when using InputAccessoryView but I didn't find any solution. Most of the solutions suggest using keyboard notifications which defeats the purpose of having InputAccessoryView for me. I started using InputAccessoryView only to avoid kb notifications as drag to dismiss keyboard interactively was difficult to implement using kb notifications.

How do we move table view up and down with InputAccessoryView? Any hint will do as I already know how to move table view up and down with kb notifications.

Storyboard setup -> https://i.stack.imgur.com/0QJyn.png

Screen recording -> https://imgur.com/a/S5Oi9VS

Snippet of relevant code

class InputAccViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var textView: UITextView!
    @IBOutlet weak var textInputView: UIView!
    // other code

    override var canBecomeFirstResponder: Bool { return true }

    override var inputAccessoryView:UIView {
       get{
           return self.textInputView
       }
   }

    override func viewDidLoad() {
        super.viewDidLoad()

        textInputView.removeFromSuperview()
        // other code
    }
     // other code tableview delegates etc...
}
1 Answers

While facing the same issue a while back I found a simple solution that worked for me. I'm putting it here so that it could be helpful for others too. What I did was just replace the UIViewController with a UITableView constraint all sides to the UIView subclass view with just a UITableViewController and use the tableView that is in build into it to populate the data. All the moving content above the keyboard feature is built right into UITableViewController. Since I did my layout programmatically it was pretty easy for me to try this out and make it work. You seem to have done it via a storyboard try this out for yourself and you'll feel relieved by the result. By the end this is implemented your code should look from this:

class InputAccViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    @IBOutlet weak var tableView: UITableView!

To this:

class InputAccViewController: UITableViewController {
    // `@IBOutlet weak var tableView: UITableView!` removed
}

Related