【问题标题】:Change constraints at runtime在运行时更改约束
【发布时间】:2020-08-08 00:29:54
【问题描述】:

我目前正在使用 UIKit 在 Swift 中制作一个消息传递应用程序。因此,我显然需要能够显示和隐藏键盘。实际上显示和隐藏它本身不是问题。

然而,一个挑战是在键盘显示/隐藏时仅将某些视图移动一定量。我有一种方法可以抓取键盘的高度,所以在这里获得正确的数字并不是什么挑战。

我正在努力移动视图本身。我在屏幕底部有一个UIView,它包含一个文本字段和按钮,当键盘出现时,我希望它向上移动。问题是视图设置了约束,当我尝试删除或重新定义这些约束时,我只是在控制台中收到很多错误。

我尝试了bottomView.removeConstraints(bottomView.constraints),然后重新定义了所有这些,我尝试了bottomView.bottomAnchor.constraint(view.bottomAnchor).isActive = false,然后激活了另一个,但我怀疑我对这些编程约束的工作原理缺乏了解。我已经看到其他几个 Stack Overflow 答案提到“bottomView.constant”或“bottomView.bottomAnchor.constant”,但似乎视图及其约束/锚点都没有这样的属性。

问题:即使UIView 已经设置了约束,如何在iOS 键盘出现时将UIView 移动固定距离?

感谢所有帮助。

【问题讨论】:

    标签: swift uiview constraints


    【解决方案1】:

    您可以使用 IQKeyboardManager。这很简单。 https://github.com/hackiftekhar/IQKeyboardManager

    或者您可以按照下面的代码进行操作

    class ViewController: UIViewController {
        @IBOutlet weak var textFieldBottomConstraint: NSLayoutConstraint!
        
        override func viewDidLoad() {
            super.viewDidLoad()
        }
        
        override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
            NotificationCenter.default.addObserver( self, selector: #selector(keyboardWillShow(notification:)), name:  UIResponder.keyboardWillShowNotification, object: nil )
        }
        override func viewWillDisappear(_ animated: Bool) {
            super.viewWillDisappear(animated)
            NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
        }
    
        @objc func keyboardWillShow( notification: Notification) {
            if let keyboardFrame: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
                var newHeight: CGFloat
                let duration:TimeInterval = (notification.userInfo![UIResponder.keyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0
                let animationCurveRawNSN = notification.userInfo![UIResponder.keyboardAnimationCurveUserInfoKey] as? NSNumber
                let animationCurveRaw = animationCurveRawNSN?.uintValue ?? UIView.AnimationOptions.curveEaseInOut.rawValue
                let animationCurve:UIView.AnimationOptions = UIView.AnimationOptions(rawValue: animationCurveRaw)
                if #available(iOS 11.0, *) {
                    newHeight = keyboardFrame.cgRectValue.height - self.view.safeAreaInsets.bottom
                } else {
                    newHeight = keyboardFrame.cgRectValue.height
                }
                let keyboardHeight = newHeight  + 16
                UIView.animate(withDuration: duration,
                               delay: TimeInterval(0),
                               options: animationCurve,
                               animations: {
                                self.textFieldBottomConstraint.constant = -keyboardHeight
                                self.view.layoutIfNeeded() },
                               completion: nil)
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-11
      • 2015-09-20
      • 1970-01-01
      • 1970-01-01
      • 2023-03-04
      • 2016-06-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多