当UITableViewController是容器视图的子视图时,UITableView不滚动

时间:2016-11-21 16:53:53

标签: ios swift uitableview uiviewcontroller uicontainerview

我有一个带容器View的UIViewController。容器视图的子项是静态tableView。 tableView的最后一个单元格有一个文本字段。在仅使用UITableViewController时,tableViewController在选择文本字段时处理tableView的移动。现在,即使键盘出现,tableView也不会自行调整。任何解决方案?

1 个答案:

答案 0 :(得分:1)

您可以使用键盘通知滚动查看表格

// Keyboard
    func registerForKeyboardNotifications() {
        NSNotificationCenter.defaultCenter().addObserver(self,
                                                         selector: #selector(keyboardWillShow),
                                                         name: UIKeyboardWillShowNotification,
                                                         object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self,
                                                         selector: #selector(keyboardWillHide),
                                                         name: UIKeyboardWillHideNotification,
                                                         object: nil)
    }

    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
        NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
    }

    func keyboardWillShow(notification: NSNotification) {
        if let userInfo = notification.userInfo {
            if let keyboardSize = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
                let keyboardHeight = keyboardSize.height
                let contentInsets: UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardHeight, 0.0)
                tableView.contentInset = contentInsets
                tableView.scrollIndicatorInsets = contentInsets
            }
        }
    }

    func keyboardWillHide(notification: NSNotification) {
       tableView.contentInset = UIEdgeInsetsZero
       tableView.scrollIndicatorInsets = UIEdgeInsetsZero
    }

他们调用viewDidLoad函数

override func viewDidLoad() {
        super.viewDidLoad()
registerForKeyboardNotifications()
}