在显示键盘时处理tableView contentInset

时间:2015-12-30 14:44:39

标签: ios swift uitableview uiscrollview

我正在构建一个示例,我可以发布评论,我每次发布时都使用tableView添加单元格。当我想发表评论时,它主要处理但是我遇到了一个问题,tableview底部与键盘高度相同但是空的,如果将其置零,则视图将向下移动然后推顶,这就是因为我和#39;当我点击textview写文字时移动整个视图。

这是tableView下的空白区域预览(键盘高度为插图):Preview of image on Google Drive

当键盘打开时,另一张图片是否显示我的底部(内容插入)= 0:Preview of second image on Google Drive

 func keyboardWillShow(sender:NSNotification){

        let userInfo: [NSObject : AnyObject] = sender.userInfo!
        keyboardSize = userInfo[UIKeyboardFrameBeginUserInfoKey]!.CGRectValue.size
        var contentInsets = UIEdgeInsets()

        if wasEmoj == true {
            if (UIInterfaceOrientationIsPortrait(UIApplication.sharedApplication().statusBarOrientation)) {
                contentInsets = UIEdgeInsetsMake(257.0, 0.0, (self.keyboardSize.height), 0.0);
            } else {
                contentInsets = UIEdgeInsetsMake(257.0, 0.0, (self.keyboardSize.width), 0.0);
            }
            self.mytableView.contentInset = contentInsets;
            self.mytableView.scrollIndicatorInsets = contentInsets
        }else {
        if (UIInterfaceOrientationIsPortrait(UIApplication.sharedApplication().statusBarOrientation)) {
            contentInsets = UIEdgeInsetsMake(215.0, 0.0,(self.keyboardSize.height), 0.0);
        } else {
            contentInsets = UIEdgeInsetsMake(215.0, 0.0, (self.keyboardSize.width), 0.0);
        }

            self.mytableView.contentInset = contentInsets;
            self.mytableView.scrollIndicatorInsets = contentInsets

        }
}

func textViewDidBeginEditing(textView: UITextView) {

    self.animateTextField(commentText, up: true, movementDistance: -215, duration: 0.3)

}
///- animate the view up or down to adapt with keyboard
func animateTextField (textField:UITextView,up:Bool,movementDistance:Int,duration:NSTimeInterval){

    let movement : Int = (up ? movementDistance : -movementDistance);
    UIView.beginAnimations("animateTextField", context: nil)
    UIView.setAnimationBeginsFromCurrentState(true)
    if up == true {
        UIView.setAnimationDuration(0.38)
    }else {
        UIView.setAnimationDuration(0.3)
    }
    self.view.frame = CGRectOffset(self.view.frame, 0, CGFloat(movement))
    UIView.commitAnimations()


}

1 个答案:

答案 0 :(得分:1)

在这种情况下,最好更改tableview的框架。

您可以在commentView的底部和底部布局指南之间保留约束的实例。

@IBOutlet weak var commentViewBottom: NSLayoutConstraint!

然后在需要时更改约束的常量。

    func keyboardWillShow(sender:NSNotification){

        if let dic = sender.userInfo {
            if let keyboardFrame = dic[UIKeyboardFrameEndUserInfoKey]?.CGRectValue {
                if let duration = dic[UIKeyboardAnimationDurationUserInfoKey]?.doubleValue {
                    commentViewBottom.constant = -keyboardFrame.height

                    UIView.animateWithDuration(duration, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.5, options: .CurveLinear, animations: { () -> Void in
                        self.view.layoutIfNeeded()
                        }, completion: nil)
                }
            }
        }
    }


    func keyboardWillHide(sender: NSNotification) {
        if let dic = sender.userInfo, let duration = dic[UIKeyboardAnimationDurationUserInfoKey]?.doubleValue {
                commentViewBottom.constant = 0

                UIView.animateWithDuration(duration, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.5, options: .CurveLinear, animations: { () -> Void in
                    self.view.layoutIfNeeded()
                    }, completion: nil)
        }
}