AutoScroll在键盘显示时向上取消阻止TextField

时间:2015-07-06 12:07:30

标签: ios swift uitableview

在Swift中,我使用\。在那里,我使用了一个UIViewController,包含10行和1个部分。每行包含UITableView。那么当底部单元格编辑开始时如何自动滚动UITextField

1 个答案:

答案 0 :(得分:0)

func keyboardWillShow(notification:NSNotification){
    var info:NSDictionary = notification.userInfo!
    var keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue()

    var keyboardHeight:CGFloat =  keyboardSize.height - 40

    var animationDuration:CGFloat = info[UIKeyboardAnimationDurationUserInfoKey] as! CGFloat

    var contentInsets: UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardHeight, 0.0);
    self.IBtableView.contentInset = contentInsets
    self.IBtableView.scrollIndicatorInsets = contentInsets
    //self.IBtableView.contentInset.bottom = keyboardHeight

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

}

override func viewDidDisappear(animated: Bool) {
    NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
     NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
    println("Fired DidDisapper")
}

override func viewWillAppear(animated: Bool) {

    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)
    println("Fired Appear")
}