简体   繁体   English

UIButton与键盘一起移动。 用户按下键时位置重置

[英]UIButton to move along with keyboard. The position resets when user presses a key

I have a function that moves the UIButton that is at the bottom of the screen with the keyboard. 我有一个函数,可以使用键盘移动位于屏幕底部的UIButton。 The problem is, when the user presses a key (Begins to write in the text field) the button goes back down. 问题是,当用户按下一个键(开始在文本字段中写入内容)时,该按钮将向下滑动。

I tried to set addBtn.translatesAutoresizingMaskIntoConstraints = true 我试图设置addBtn.translatesAutoresizingMaskIntoConstraints = true

This works only if my Xcode has the main.storyboard set as the simulator, otherwise it breaks the constraints. 仅当我的Xcode将main.storyboard设置为模拟器时,此方法才有效,否则会打破约束。 At first I thought it's just a Xcode 9 bug but then I uploaded a bundle on iTunes Connect and asked my girlfriend to download the app through TestFlight and indeed the constraints were messed up when setting the translatesAutoresizingMaskIntoConstraints = true 一开始我以为这只是Xcode 9的错误,但后来我在iTunes Connect上上传了一个捆绑包,并问我的女友通过TestFlight下载该应用程序,而在设置translatesAutoresizingMaskIntoConstraints = true时,约束确实搞砸了

func bindToKeyboard () {
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(_:)), name: NSNotification.Name.UIKeyboardWillChangeFrame , object: nil)
}

@objc func keyboardWillChange(_ notification : NSNotification) {
    let duration = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! Double
    let curve = notification.userInfo![UIKeyboardAnimationCurveUserInfoKey] as! UInt
    let startingFrame = (notification.userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
    let endingFrame = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue

    let deltaY = endingFrame.origin.y - startingFrame.origin.y

    UIView.animateKeyframes(withDuration: duration, delay: 0.0, options: UIViewKeyframeAnimationOptions(rawValue: curve), animations: {self.frame.origin.y += deltaY
    },completion: nil)
}

OK so after 3 hours of fighting with this bug I disabled the autoresizingmasks and I created an outlet for the bottom constraint . 好吧,在与这个bug战斗了3个小时之后,我禁用了自动调整大小掩码,并为底部约束创建了出口

After every animation I updated that constraint with deltaY 在每个动画之后,我都用deltaY更新了该约束

so my code looks like this now 所以我的代码现在看起来像这样

func bindToKeyboard () {
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(_:)), name: NSNotification.Name.UIKeyboardWillChangeFrame , object: nil)
}

@objc func keyboardWillChange(_ notification : NSNotification) {
    let duration = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! Double
    let curve = notification.userInfo![UIKeyboardAnimationCurveUserInfoKey] as! UInt
    let startingFrame = (notification.userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
    let endingFrame = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue

    let deltaY = endingFrame.origin.y - startingFrame.origin.y

    UIView.animateKeyframes(withDuration: duration, delay: 0.0, options: UIViewKeyframeAnimationOptions(rawValue: curve), animations: {self.frame.origin.y += deltaY
    },completion: nil)


    btc.constant -= deltaY
}

What a pleasant saturday night! 多么令人愉快的星期六晚上! I love programming! 我喜欢编程!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM