gpt4 book ai didi

ios - UIButton 随键盘一起移动。当用户按下一个键时位置重置

转载 作者:行者123 更新时间:2023-11-28 07:58:45 24 4
gpt4 key购买 nike

我有一个功能,可以使用键盘移动屏幕底部的 UIButton。问题是,当用户按下一个键(开始在文本字段中写入)时,按钮会返回。

我尝试设置 addBtn.translatesAutoresizingMaskIntoConstraints = true

这仅在我的 Xcode 将 main.storyboard 设置为模拟器时有效,否则它会破坏约束。起初我以为这只是 Xcode 9 的一个错误,但后来我在 iTunes Connect 上上传了一个 bundle ,并让我的女朋友通过 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)
}

最佳答案

好吧,在与这个错误斗争了 3 个小时之后,我禁用了自动调整大小掩码,并为底部约束 创建了一个导出

在每个动画之后我用 deltaY 更新了那个约束

所以我的代码现在看起来像这样

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
}

多么愉快的周六晚上啊!我喜欢编程!

关于ios - UIButton 随键盘一起移动。当用户按下一个键时位置重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47244166/

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