【发布时间】: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没有显示(高度没有改变)
这是我尝试过的:
-
我在 ViewController 类中声明了变量(在 viewDidLoad 之前)
var errorViewHeight = NSLayoutConstraint()
-
在函数的开头,我指定了它。
errorViewHeight = NSLayoutConstraint(item:errorView, attribute:NSLayoutAttribute.Height, relatedBy:NSLayoutRelation.Equal, toItem:nil, attribute:NSLayoutAttribute.NotAnAttribute, multiplier:1.0, constant:0)
-
最后,在 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