【问题标题】:Changing constraint after adding it causes constraint error (swift)添加后更改约束会导致约束错误(swift)
【发布时间】:2015-05-06 04:10:19
【问题描述】:

我正在努力将错误消息添加到我的登录屏幕。

虽然代码运行良好,并且执行我希望它执行的操作。它会导致执行时出现约束错误。

以下是受影响的约束:

self.view.addConstraints(
    NSLayoutConstraint.constraintsWithVisualFormat(
        "V:|[topBar]-32-[emailInputField]-32-[passwordInputField]-32-[signInButton]-16-[resetPasswordButton]-[signUpButton]", options: nil, metrics: nil, views: viewsDictionary))
self.view.addConstraints(
    NSLayoutConstraint.constraintsWithVisualFormat(
        "V:|[topBar]-32-[emailIconBox]-32-[passwordIconBox]", options: nil, metrics: nil, views: viewsDictionary))

这是导致errorView出现的函数。

// sign in button function
func signIn(sender: UIButton) {
self.view.endEditing(true)

if emailInputField.text.isEmpty {
    println("NO EMAIL")

    errorLabel.attributedText = UILabel.ErrorMessage("Form fields cannot be empty!")


    var viewsDictionary = [ "topBar":topBar,
        "errorView":errorView,
        "errorLabel":errorLabel,
        "emailInputField":emailInputField,
        "emailIconBox":emailIconBox,
        "passwordInputField":passwordInputField,
        "passwordIconBox":passwordIconBox,
        "signInButton":signInButton,
        "resetPasswordButton":resetPasswordButton,
        "signUpButton":signUpButton]

    self.view.addConstraints(
        NSLayoutConstraint.constraintsWithVisualFormat(
            "V:|[topBar]-16-[errorView]-16-[emailInputField]-32-[passwordInputField]-32-[signInButton]-16-[resetPasswordButton]-[signUpButton]", options: nil, metrics: nil, views: viewsDictionary))
    self.view.addConstraints(
        NSLayoutConstraint.constraintsWithVisualFormat(
            "V:|[topBar]-16-[errorView]-16-[emailIconBox]-32-[passwordIconBox]", options: nil, metrics: nil, views: viewsDictionary))
    self.view.addConstraints(
        NSLayoutConstraint.constraintsWithVisualFormat(
            "V:[errorView(50)]", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDictionary))
}
}

如何在不破坏约束的情况下更改约束?

我尝试了 self.view.updateConstraints() - 但什么也没做。 我还尝试在添加约束之前删除它们,但仍然出现错误。

任何帮助将不胜感激!

编辑:

我找到了一个objective-c解决方案,用一个常量来约束一个变量,然后改变这个常量。 - 我试着把它翻译成swift。我运行它时没有错误,但我的errorView没有显示(高度没有改变)

这是我尝试过的:

  1. 我在 ViewController 类中声明了变量(在 viewDidLoad 之前)

    var errorViewHeight = NSLayoutConstraint()

  2. 在函数的开头,我指定了它。

    errorViewHeight = NSLayoutConstraint(item:errorView, attribute:NSLayoutAttribute.Height, relatedBy:NSLayoutRelation.Equal, toItem:nil, attribute:NSLayoutAttribute.NotAnAttribute, multiplier:1.0, constant:0)

  3. 最后,在 if 语句返回 true 后,我尝试对其进行动画处理。

    UIView.animateWithDuration(0.2, animations: { () -> Void in self.errorViewHeight.constant = 50 self.view.layoutIfNeeded() })

编辑:

这是错误信息:

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. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x170089e20 V:[UIView:0x174191780(0)]>",
    "<NSLayoutConstraint:0x170089fb0 V:[UIView:0x174191780(50)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x170089fb0 V:[UIView:0x174191780(50)]>

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.

【问题讨论】:

  • 您可以更改约束而不会破坏它们,只需确保您没有破坏它们。开个玩笑,你能告诉我们你得到的错误吗?
  • 错误视图从何而来?你是添加它的代码,还是在 IB 中?错误是说您将高度设置为 0 和 50。
  • 看起来您正在将多个垂直约束设置为“errorView”。在前两个“addConstraints”中,“errorView”高度没有设置约束。最后,您将高度设置为 50。我相信这些是造成冲突的原因。可以去掉最后一个“addConstraints”,改为在上面两个中设置高度。
  • 我知道是什么导致了冲突,我只是不知道如何在不添加约束的情况下更改约束。我已经挣扎了大约10个小时。我找到了一个我试图翻译的 obj-c 解决方案。我会用它来编辑我的帖子。 (基本上和你说的一样,但我不能让它工作)

标签: ios swift constraints


【解决方案1】:

我想出了如何使用常量来更改约束。

在 viewController 类中我定义了约束:

var errorViewHeight:NSLayoutConstraint!

在 viewDidLoad 中,我指定并将其添加到我的 UIView 中

errorViewHeight = NSLayoutConstraint(item:errorView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier:1.0, constant:0)
errorView.addConstraint(errorViewHeight)

从那时起,我可以使用以下代码行正确更改高度

errorViewHeight.constant = 50

希望对寻找同样问题的人有所帮助。

【讨论】:

    【解决方案2】:

    您可以通过添加以下扩展来更改任何约束,如果您要部署到多个设备,那就太好了:

    extension UIView
    {
        func updateHieghtForView()
        {
            var constant = CGFloat()
            // change the constant by switching device type by screen size 
            let allConstraints = self.constraints
            for heightConstraint in allConstraints
            {
                if heightConstraint.firstAttribute == .Height 
                {
                    self.removeConstraint(heightConstraint)
                }
            }
            let layOutConstaint = NSLayoutConstraint(item: self,
                                                     attribute: .Height,
                                                     relatedBy: .Equal,
                                                     toItem: nil,
                                                     attribute:.Height,
                                                     multiplier: 1,
                                                     constant: constant)
            self.addConstraint(layOutConstaint)
            self.updateConstraints()
        }
    }
    

    【讨论】:

      【解决方案3】:

      我想补充的是,有些人有在 viewDidLayout 方法中设置约束的习惯,所以当你更改或编辑任何约束时,它会给你这个错误,因为 viewDidLayout 不像 viewDidLoad 或其他功能。所以我的建议是,如果你在做这样的事情,请使用 viewDidAppear 而不是 viewDidLayout,最好是使用 viewDidLoad。 p>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多