在出现键盘时处理 tableView contentInset
Posted
技术标签:
【中文标题】在出现键盘时处理 tableView contentInset【英文标题】:Handle tableView contentInset while keyboard is presented 【发布时间】:2015-12-30 14:44:38 【问题描述】:我正在构建一个示例,我可以在其中发布 cmets,我每次发布时使用 tableView 添加一个单元格。它主要处理但我想发表评论时遇到一个问题,tableview底部与键盘高度相同但为空,如果将其设置为零,视图将向下移动然后推到顶部,那是因为我当我点击 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】:这种情况下最好改变tableview的frame。
您可以将约束的实例保留在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)
【讨论】:
以上是关于在出现键盘时处理 tableView contentInset的主要内容,如果未能解决你的问题,请参考以下文章
当键盘出现时,如何阻止 tableView 滚动? [复制]
tableview 中的两个 TextFields,都在滚动视图上,当键盘出现 iPhone 时消失