美文网首页
swift 键盘防遮挡cell

swift 键盘防遮挡cell

作者: chinwy | 来源:发表于2016-11-08 17:18 被阅读68次

...

func registerForKeyboardNotifications() {

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(keyboardWillShow(_:)), name:UIKeyboardWillShowNotification, object: nil)

    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(keyboardWillBeHidden(_:)), name:UIKeyboardWillHideNotification, object: nil)
}

func keyboardWillShow(aNotification: NSNotification) {
    
    let info : NSDictionary = aNotification.userInfo!
    // 注意不要用UIKeyboardFrameBeginUserInfoKey,第三方键盘可能会存在高度不准,相差40高度的问题
    let kbSize :CGSize = (info.objectForKey(UIKeyboardFrameEndUserInfoKey)?.CGRectValue()
        .size)!
    // 修改滚动天和tableView的contentInset
    self.convenientTV.contentInset = UIEdgeInsetsMake(0, 0, kbSize.height, 0)
    self.convenientTV.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, kbSize.height, 0)
    
    // 跳转到当前点击的输入框所在的cell
    UIView.animateWithDuration(0.2) {
        self.convenientTV.scrollToRowAtIndexPath(self.indexPath, atScrollPosition: .Top, animated: false)
    }
    
}

func keyboardWillBeHidden(aNotification: NSNotification) {
    self.convenientTV.contentInset = UIEdgeInsets(top: -25, left: 0, bottom: 0, right: 0)
    self.convenientTV.scrollIndicatorInsets = UIEdgeInsets(top: -25, left: 0, bottom: 0, right: 0)
}

func textFieldDidBeginEditing(textField: UITextField) {
    let indexPath = NSIndexPath.init(forRow: textField.tag, inSection: 0)
    self.indexPath = indexPath

    
}

func textFieldShouldEndEditing(textField: UITextField) -> Bool {

    return true
}


deinit {
    NSNotificationCenter.defaultCenter().removeObserver(self)
    NSLog("LoginViewController deinits")
}

...

相关文章

网友评论

      本文标题:swift 键盘防遮挡cell

      本文链接:https://www.haomeiwen.com/subject/zpaauttx.html