UIScrollViewKeyboardDismissModeInteractive changing tableview height with keyboard

怎甘沉沦 提交于 2019-12-21 17:47:00

问题


Within a UIViewController I've set self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive.

This is great because the user can drag the keyboard down from the tableview.

However, the tableview maintains it's current height when dragging down the keyboard. This looks odd because it leaves empty space between the keyboard and the scrollview.

How can I persistently track the frame of the keyboard so I may resize the tableview as the user drags the keyboard? I've tried using UIKeyboardWillChangeFrameNotification but that seems to only get called after the user finishes dragging.


回答1:


Your table view shouldn't change its height to accommodate the keyboard.

Instead, the keyboard should be presented overtop of the table view, and you should adjust the contentInset and scrollIndicatorInsets properties on the table view so that the lower table content is not obscured by the keyboard. You need to update the scroll insets whenever the keyboard is presented or dismissed.

You don't have to do anything special while the keyboard is dismissed interactively, because the table content should already scroll down as the keyboard moves out of view.




回答2:


I'd rather this not be the accepted answer, but for those of you out there also having trouble with this here's what worked for me.

  1. Create a custom subclass of UIView.

  2. In the subclass's willMoveToSuperview: method, add a KVO observer to the superview's center on iOS 8 and frame on lesser versions of iOS (remember to remove the old observer, you may want to use an instance variable to track this).

  3. In your view controller add a 0.0 height input accessory view to the view controller via inputAccessoryView class override. Use your custom UIView subclass for this.

  4. Back in the subclass, in observeValueForKeyPath:..., capture the frame of the view's superview, this should be the frame of the UIKeyboard.

  5. Use NSNotificationCenter or some other means to then pass this frame back to your view controller for processing.

It's a pretty painful process and not guaranteed to work in future versions of iOS. There are likely a ton of edge cases that will pop up later since I just built this, but it's a good start. If anyone has a better idea I'll happily mark your answer as correct.




回答3:


This is what I come up to, insted of using notification I use a delegate:

protocol MyCustomViewDelegate {
    func centerChanged(center: CGPoint)
}
class MyCustomView: UIView{

    private var centerContext = 0
    var delegate: MyCustomViewDelegate?
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    override func willMoveToSuperview(newSuperview: UIView?) {
        super.willMoveToSuperview(newSuperview)
        guard let newSuperview = newSuperview else {
            self.superview?.removeObserver(self, forKeyPath: "center")
            return
        }
        let options = NSKeyValueObservingOptions([.New, .Old])
        newSuperview.addObserver(self, forKeyPath: "center", options: options, context: &centerContext)
    }
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {

        //print("CHANGE",keyPath)


        if context == &centerContext {

            guard let center = superview?.center else {
                super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
                return
            }

            delegate?.centerChanged(center)

        } else {
            super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
        }
    }
}


来源:https://stackoverflow.com/questions/27556264/uiscrollviewkeyboarddismissmodeinteractive-changing-tableview-height-with-keyboa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!