简体   繁体   English

如何更新以前在代码中设置的NSLayoutConstraint常量? -迅捷

[英]How to update NSLayoutConstraint constants, that have previously been set in code? - Swift

I'm trying to completely make my view programatically. 我试图以编程方式完全表达我的观点。 It has a bottom view that animates up when a search query finishes with the relevant information. 它具有一个底视图,当搜索查询完成相关信息后,该视图会向上移动。 I've set the .bottom nslayoutconstraint of the bottom view to an optional nslayoutconstraint, first initialising it off screen in override func viewWillLayoutSubviews(). 我已经将底部视图的.bottom nslayoutconstraint设置为可选的nslayoutconstraint,首先在屏幕上的覆盖函数func viewWillLayoutSubviews()中对其进行了初始化。

    let bottomView = UIView()
    var bottomViewBottomConstraint: NSLayoutConstraint!

override func viewDidLoad() {
    super.viewDidLoad()
    view.addSubview(bottomView)

}

override func viewWillLayoutSubviews() {

    //Bottom View Constraints
    bottomView.translatesAutoresizingMaskIntoConstraints = false
    let bottomViewLeftConstraint = NSLayoutConstraint(item: bottomView, attribute: .left, relatedBy: .equal, toItem: view, attribute: .left, multiplier: 1, constant: 0)
    let bottomViewRightConstraint = NSLayoutConstraint(item: bottomView, attribute: .right, relatedBy: .equal, toItem: view, attribute: .right, multiplier: 1, constant: 0)
    let bottomViewHeightConstraint = NSLayoutConstraint(item: bottomView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 75)
    bottomViewBottomConstraint = NSLayoutConstraint(item: self.bottomView, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1, constant: 100)

    NSLayoutConstraint.activate([
        bottomViewLeftConstraint, bottomViewRightConstraint, bottomViewBottomConstraint, bottomViewHeightConstraint
        ])
   }

The view appears up once the search query is complete. 搜索查询完成后,该视图将出现。

                self.view.layoutIfNeeded()

            UIView.animate(withDuration: 0.8, delay: 1, usingSpringWithDamping: 1, initialSpringVelocity: 0.1, options: .curveEaseIn, animations: {
                    self.bottomViewBottomConstraint.constant = -5
                    self.view.layoutIfNeeded()
                }, completion: nil)

However after that point, when I try to dismiss the view, and change the NsLayoutConstraint's constant, the view doesn't move. 但是在那之后,当我尝试关闭视图并更改NsLayoutConstraint的常量时,视图不会移动。

    self.view.layoutIfNeeded()
    UIView.animate(withDuration: 0.8, animations: {
        self.bottomViewBottomConstraint.constant = 100
        self.view.layoutIfNeeded()
    })

In the output console, I'm getting this error and I'm not sure how to fix it. 在输出控制台中,我收到此错误,并且不确定如何解决。

    Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to      catch this in the debugger.
    The methods in the UIConstraintBasedLayoutDebugging category on UIView   listed in <UIKit/UIView.h> may also be helpful.
    2016-10-11 14:50:01.768353 Green Homes Australia[973:232019]   [LayoutConstraints] Unable to simultaneously satisfy constraints.
        Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
(
    "<NSLayoutConstraint:0x17028d6b0 UIView:0x1049365b0.bottom == UIView:0x100b04e40.bottom - 5   (active)>",
    "<NSLayoutConstraint:0x174681cc0 UIView:0x1049365b0.bottom == UIView:0x100b04e40.bottom + 100   (active)>")

    Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x174681cc0 UIView:0x1049365b0.bottom == UIView:0x100b04e40.bottom + 100   (active)>

The problem is that your 问题是你

override func viewWillLayoutSubviews() {

runs again and again and again. 一次又一次地运行。 Layout happens a lot! 布局经常发生! Thus, those constraints all get created and added again and again, including after you have done the animation and changed the bottom constraint. 因此,所有这些约束都会一次又一次地创建和添加,包括完成动画并更改底部约束之后。 So you end up with two conflicting bottom constraints. 因此,您最终遇到了两个相互矛盾的底部约束。 (You actually have lots of bottom constraints, but all the others are equal to one of the the conflicting constraints so the runtime doesn't complain.) (实际上,您有很多底部约束,但是所有其他约束都等于冲突的约束之一,因此运行时不会抱怨。)

A simple solution that I like to use is to put a boolean flag so that my initial configuration only happens once: 我喜欢使用的一个简单解决方案是放置一个布尔标志,以便我的初始配置仅发生一次:

var didConfig = false
override func viewWillLayoutSubviews() {
    if !didConfig {
        didConfig = true
        // create and configure constraints here
    }
}

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

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