【问题标题】:UITableView not scrolling when UITableViewController is a child view of a container view当 UITableViewController 是容器视图的子视图时,UITableView 不滚动
【发布时间】:2017-04-05 03:42:03
【问题描述】:

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

【问题讨论】:

    标签: ios swift uitableview uiviewcontroller uicontainerview


    【解决方案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()
    }
    

    【讨论】:

    • 我已经在别处实现了这个。是否可以让 TableViewController 处理它而不是使用键盘通知?
    • @Watson 你可以在 ScrollViewDelegate 中实现,没问题。 TableView 是 ScrollView 的子类。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 2012-06-29
    • 1970-01-01
    • 1970-01-01
    • 2015-12-08
    • 1970-01-01
    相关资源
    最近更新 更多