【发布时间】:2017-01-28 23:21:51
【问题描述】:
上下文:
我有一个包含 UITextView 的 UIView。当键盘可见时,我想将该 UIView 向上移动。所以这个想法是change the Y position。
问题:
有时,当键盘仍然可见时,UIView 会回到其初始 Y 位置。即使我专注于输入,它也不会再回来了。我真的不知道为什么。
插图 (gif):
https://d17oy1vhnax1f7.cloudfront.net/items/2K0z3P1j1x2f0X2X1B2v/ci.gif
PS:键盘在 Gif 中出现 3 次。它是循环的,所以很难区分结尾。
代码
我做了什么,首先我添加了通知观察者:
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillChangeFrame), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
然后我定义了键盘切换处理程序:
func keyboardWillChangeFrame(notification: NSNotification) {
let info = notification.userInfo!
let keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
self.keyboardInfo["isVisible"] = self.view.frame.size.height - keyboardFrame.origin.y != 0
self.keyboardInfo["height"] = keyboardFrame.size.height
self.keyboardInfo["animationDuration"] = info[UIKeyboardAnimationDurationUserInfoKey] as! Double
self.keyboardInfo["animationCurve"] = info[UIKeyboardAnimationCurveUserInfoKey] as! UInt
if self.keyboardInfo["isVisible"] as! Bool {
self.moveCommentInputUp()
} else {
self.moveCommentInputDown()
}
}
之后,我定义了上下移动评论输入视图的方法:
func moveCommentInputUp() {
print("Moving up... ", self.commentFormView.frame.origin.y, " to ", self.view.frame.size.height - (self.keyboardInfo["height"] as! CGFloat) - self.commentFormView.frame.size.height)
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDuration(self.keyboardInfo["animationDuration"] as! TimeInterval)
UIView.setAnimationCurve(UIViewAnimationCurve(rawValue: Int(self.keyboardInfo["animationCurve"] as! UInt))!)
UIView.setAnimationBeginsFromCurrentState(true)
ViewUtil.changeViewFrame(view: self.commentFormView, yPosition: self.view.frame.size.height - (self.keyboardInfo["height"] as! CGFloat) - self.commentFormView.frame.size.height)
ViewUtil.removeShadowToView(self.commentFormLauncherButton)
UIView.commitAnimations()
}
func moveCommentInputDown() {
print("Moving down... ", self.commentFormView.frame.origin.y, " to ", self.view.frame.size.height)
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDuration(self.keyboardInfo["animationDuration"] as! TimeInterval)
UIView.setAnimationCurve(UIViewAnimationCurve(rawValue: Int(self.keyboardInfo["animationCurve"] as! UInt))!)
UIView.setAnimationBeginsFromCurrentState(true)
ViewUtil.changeViewFrame(view: self.commentFormView, yPosition: self.view.frame.size.height)
ViewUtil.addShadowToView(commentFormLauncherButton, position: CGSize(width: 0, height: 5))
UIView.commitAnimations()
}
按钮的动作是
@IBAction func showCommentForm(_ sender: UIButton) {
self.commentInput.becomeFirstResponder()
}
日志(请参阅 Gif)显示视图从 568(屏幕底部)再次移动到 568
// The numbers show the initial and the final value of the keyboard Y position
// Click on button to focus the input
Moving up... 568.0 to 282.0
// Tap anywhere
Moving down... 282.0 to 568.0
// Click again on button to focus the input
Moving up... 568.0 to 282.0
///// Write "hhh". The view is gone, I don't know why.
// Tap anywhere
Moving down... 568.0 to 568.0 // The view seems to move from 568 to 568, hugh
// Click on button to focus the input
Moving up... 568.0 to 282.0 // This didn't work apparently
// Tap anywhere
Moving down... 568.0 to 568.0 // 568 to 568 again
我错过了什么吗?
系统:iOS 9、Xcode 8、Swift 3
【问题讨论】: