2016-11-21 97 views
2

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

回答

0

您可以使用键盘通知滚动实现代码如下起来

// 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() 
} 
+0

我已经在其他地方实现这一点。是否有可能使TableViewController处理它而不是使用键盘通知? – MrDank

+0

@Watson你可以使它在ScrollViewDelegate中,没问题。 TableView是ScrollView的子类。 –

相关问题