【问题标题】:Button disappears after animating it按钮在动画后消失
【发布时间】:2018-01-31 22:48:01
【问题描述】:

我正在使用 UIView Extension for button 来用键盘向上滑动它。

extension UIView {
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

    let options = UIViewAnimationOptions(rawValue: curve << 16)

    UIView.animate(withDuration: duration, delay: 0, options: options, animations: {
        self.frame.origin.y += deltaY
        self.layoutIfNeeded()
    }, completion: nil)
  }
}

然后在 ViewController 中使用:

func setUpView() {
        okayButton.bindToKeyboard()
        self.isHeroEnabled = true
    }

但问题是当我按下屏幕上的其他按钮时:

点击其他按钮后,保存按钮在“上部位置”时消失,在底部时出现。我究竟做错了什么?如何预防/修复它?

编辑:这些按钮中的任何一个都没有任何操作! (+,-,保存)

谢谢!

【问题讨论】:

  • 键盘问题没有隐藏。
  • 这些按钮没有任何动作。
  • 您是否尝试过对视图应用变换而不是调整框架?我感觉这与调整框架和重新计算布局不正确有关。
  • 显示“+”按钮操作代码和情节提要绑定快照。

标签: ios swift animation button


【解决方案1】:

您不一定需要更新 self.view 。您可以做的是为保存按钮创建一个IBOutlet 底部间距。

@IBOutlet weak var saveButtonBottomSpacing: NSLayoutConstraint!
  1. 当键盘打开时,将底部间距常量设置为键盘的高度。
  2. 当键盘关闭时,恢复底部间距。可以是 0 或您想要的值。

您可以在 UIView 动画块中进行此更改。

【讨论】:

  • 我能以某种方式访问​​ UIView 扩展中的约束吗?
  • 如果您的保存按钮在该视图内,是的。
【解决方案2】:

在“保存”按钮成功操作后隐藏(退出)您的键盘。

这是您需要在“保存”按钮操作中更新的示例代码。

@IBAction func btnSave(sender: Any){

    // add this line in your upon, successful action on save button
    yourTextView.resignFirstResponder()
}

【讨论】:

  • 没有解决问题。点击保存按钮后我会辞职,但问题是当用户点击“+”时,他无法用屏幕上的键盘保存它,因为没有保存按钮。
  • 显示“+”按钮代码及其情节提要连接快照。
猜你喜欢
  • 1970-01-01
  • 2015-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-21
  • 1970-01-01
  • 2011-06-09
相关资源
最近更新 更多