以编程方式设置后iOS约束未更新

时间:2017-05-25 03:06:25

标签: ios xcode swift3 autolayout

我试图在加载时定位所有ui对象,但它似乎没有改变。从上到下,布局是:主机,端口,用户和密码,我对齐:
*中间的中间视图
*顶部布局边缘的主机顶部
*主机底部的港口顶部
*端口左下方的用户顶部
*用户底部的密码顶部
有人可以帮忙吗?这是代码。谢谢!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    self.view.translatesAutoresizingMaskIntoConstraints = false
    host.centerXAnchor.constraint(equalTo:self.view.centerXAnchor).isActive = true
    host.topAnchor.constraint(equalTo:self.view.layoutMarginsGuide.topAnchor, constant:20.0).isActive = true
    port.centerXAnchor.constraint(equalTo:self.view.centerXAnchor).isActive = true
    port.topAnchor.constraint(equalTo:host.bottomAnchor, constant:20.0).isActive = true
    user.centerXAnchor.constraint(equalTo:self.view.centerXAnchor).isActive = true
    user.topAnchor.constraint(equalTo:port.bottomAnchor, constant:20.0).isActive = true
    password.centerXAnchor.constraint(equalTo:self.view.centerXAnchor).isActive = true
    password.topAnchor.constraint(equalTo:user.bottomAnchor, constant:20.0).isActive = true
    connectButton.centerXAnchor.constraint(equalTo:self.view.centerXAnchor).isActive = true
    connectButton.topAnchor.constraint(equalTo:password.bottomAnchor, constant:20.0).isActive = true
    self.view.setNeedsUpdateConstraints()
    self.view.setNeedsLayout()
    self.view.updateConstraints()
}

然后我在控制台中收到了很多错误:

[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.
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSAutoresizingMaskLayoutConstraint:0x174288930 h=--& v=--& UITextField:0x10141f240.midY == 256   (active)>",
"<NSAutoresizingMaskLayoutConstraint:0x174288980 h=--& v=--& UITextField:0x10141f240.height == 30   (active)>",
"<NSAutoresizingMaskLayoutConstraint:0x17028dac0 h=--& v=--& UIButton:0x10131e3a0'Connect'.midY == 325.5   (active)>",
"<NSAutoresizingMaskLayoutConstraint:0x17028db10 h=--& v=--& UIButton:0x10131e3a0'Connect'.height == 45   (active)>",
"<NSLayoutConstraint:0x174287df0 V:[UITextField:0x10141f240]-(20)-[UIButton:0x10131e3a0'Connect']   (active)>"
)

Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x174287df0 V:[UITextField:0x10141f240]-(20)-[UIButton:0x10131e3a0'Connect']   (active)>

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.

1 个答案:

答案 0 :(得分:0)

此处的问题是,您未在小部件translatesAutoresizingMaskIntoConstraintshostportuser和{{1}上关闭password因此,对于这些,将生成与您的约束相冲突的connectButton

你可以插入这个

NSAutoresizingMaskLayoutConstraint

for view in [host, port, user, password, connectButton] as [UIView] { view.translatesAutoresizingMaskIntoConstraints = false } 之后的viewDidLoad方法中。

您还可以删除尝试在最后考虑约束的尝试,以便您的代码如下所示:

self.view.translatesAutoresizingMaskIntoConstraints = false

然后你会意识到你可能想在你的(我想的是)文本字段上添加一些宽度限制;)。

无论如何,如果您希望在Interface Builder中停用override func viewDidLoad() { super.viewDidLoad() self.view.translatesAutoresizingMaskIntoConstraints = false for view in [host, port, user, password, connectButton] as [UIView] { view.translatesAutoresizingMaskIntoConstraints = false } host.centerXAnchor.constraint(equalTo:self.view.centerXAnchor).isActive = true host.topAnchor.constraint(equalTo:self.view.layoutMarginsGuide.topAnchor, constant:20.0).isActive = true port.centerXAnchor.constraint(equalTo:self.view.centerXAnchor).isActive = true port.topAnchor.constraint(equalTo:host.bottomAnchor, constant:20.0).isActive = true user.centerXAnchor.constraint(equalTo:self.view.centerXAnchor).isActive = true user.topAnchor.constraint(equalTo:port.bottomAnchor, constant:20.0).isActive = true password.centerXAnchor.constraint(equalTo:self.view.centerXAnchor).isActive = true password.topAnchor.constraint(equalTo:user.bottomAnchor, constant:20.0).isActive = true connectButton.centerXAnchor.constraint(equalTo:self.view.centerXAnchor).isActive = true connectButton.topAnchor.constraint(equalTo:password.bottomAnchor, constant:20.0).isActive = true } ,请参阅this answer on "Unexpected NSAutoresizingMaskLayoutConstraint adding UIView from nib to autolayout storyboard scene"