gpt4 book ai didi

ios - 在出现键盘时处理 tableView contentInset

转载 作者:行者123 更新时间:2023-11-28 06:52:24 26 4
gpt4 key购买 nike

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

这是 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()


}

最佳答案

在这种情况下,最好更改表格 View 的框架。

您可以保留 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)
}
}

关于ios - 在出现键盘时处理 tableView contentInset,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34531657/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com