键盘可见时更改Y原点时的奇怪UIView行为

Dav*_*vid 5 uiview ios swift

语境:

我有一个包含UITextView的UIView.我想在键盘可见时移动UIView.所以我的想法是改变Y的位置.

问题:

有时,当键盘仍然可见时,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)
Run Code Online (Sandbox Code Playgroud)

然后我定义了键盘切换处理程序:

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()
    }

}
Run Code Online (Sandbox Code Playgroud)

之后,我已经定义了在注释输入视图中向上/向下移动的方法:

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

}
Run Code Online (Sandbox Code Playgroud)

按钮的动作是

@IBAction func showCommentForm(_ sender: UIButton) {

    self.commentInput.becomeFirstResponder()

}
Run Code Online (Sandbox Code Playgroud)

日志(请参阅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
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

系统:iOS 9,Xcode 8,Swift 3

Sco*_*how 4

好的,这是一个不需要 CocoaPods 的解决方案。

我的方法涉及在键盘出现和消失时设置文本字段底部约束的常量。这意味着您需要创建对文本字段底部约束的引用。

@IBOutlet weak var textFieldBottomConstraint: NSLayoutConstraint!
Run Code Online (Sandbox Code Playgroud)

我首先在 viewDidLoad 中的 NSNotification.Name.UIKeyboardWillShow 和 UIKeyboardWillHide 上设置观察者,而不是 UIKeyboardWillChangeFrame:

NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardNotification), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardNotification), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
Run Code Online (Sandbox Code Playgroud)

接下来,我设置选择器及其函数来设置文本字段底部约束的常量:

func handleKeyboardNotification(notification: NSNotification) {
    if let userInfo = notification.userInfo {
        let keyboardFrameValue = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)
        let keyboardFrame = keyboardFrameValue?.cgRectValue

        let isKeyboardShowing = notification.name == NSNotification.Name.UIKeyboardWillShow

        textFieldBottomConstraint.constant = isKeyboardShowing ? keyboardFrame!.height + 4 : 4
        self.view.layoutIfNeeded()
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意,textFieldBottomConstraint 是底部约束的引用变量。

这样做的作用是使 textField 在键盘出现或消失时设置其底部约束,并且不会遇到您遇到的奇怪故障。

希望这对您有所帮助,请随时要求澄清!

感谢 YouTuber 让我们构建该应用程序!通过此视频提供解决方案:

https://www.youtube.com/watch?v=p8IaS5lmhuM