Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UI按钮可随键盘移动。当用户按键时,位置复位_Ios_Swift_Uibutton_Uikeyboard - Fatal编程技术网

Ios UI按钮可随键盘移动。当用户按键时,位置复位

Ios UI按钮可随键盘移动。当用户按键时,位置复位,ios,swift,uibutton,uikeyboard,Ios,Swift,Uibutton,Uikeyboard,我有一个功能,可以用键盘移动屏幕底部的UIButton。问题是,当用户按下一个键(开始在文本字段中写入)时,按钮会返回 我试图将addBtn.translatesAutoresizingMaskIntoConstraints=true 只有当我的Xcode将main.storyboard设置为模拟器时,这才有效,否则它会打破限制。起初我以为这只是一个Xcode 9错误,但后来我在iTunes Connect上上传了一个捆绑包,并要求我的女朋友通过TestFlight下载该应用程序,事实上,在设

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

我试图将
addBtn.translatesAutoresizingMaskIntoConstraints=true

只有当我的Xcode将main.storyboard设置为模拟器时,这才有效,否则它会打破限制。起初我以为这只是一个Xcode 9错误,但后来我在iTunes Connect上上传了一个捆绑包,并要求我的女朋友通过TestFlight下载该应用程序,事实上,在设置
translatesAutoResistinggmaskintoConstraints=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)
}

好的,在与这个bug斗争3个小时后,我禁用了AutoResizingSks,并为底部约束创建了一个出口

每次动画之后,我都会用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
}

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

好的,在与这个bug斗争3个小时后,我禁用了AutoResizingSks,并为底部约束创建了一个出口

每次动画之后,我都会用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
}

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

尝试使用keyboardWillShow和keyboardWillHide,而不是keyboardWillChange通知。或者,查看键盘的附件视图,使动画更易于管理,键盘显示和消失。当前的逻辑是keyboardWillChange,因为我有两个文本字段,一个数字字段和一个字母字段,所以键盘不同,它需要随着键盘更改移动按钮。我试过你说的,但还是不起作用,当我按下键盘上的一个键时,按钮会返回到有限制的位置尝试使用keyboardWillShow和keyboardWillHide,而不是keyboardWillChange通知。或者,查看键盘的附件视图,使动画更易于管理,键盘显示和消失。当前的逻辑是keyboardWillChange,因为我有两个文本字段,一个数字字段和一个字母字段,所以键盘不同,它需要随着键盘更改移动按钮。我试过你说的话,但还是不起作用,当我按键盘上的一个键时,按钮会返回到有限制的地方