iOS约束在编程设置后未更新
问题描述:
我试图在加载时定位所有UI对象,但似乎没有改变。从上到下,布局为:主机,端口,用户和密码,与我对齐:
*中间全部与视图中间
*顶部布局裕量的主机顶部
*端口顶部主机底部
*顶部用户底部端口
*用户底部密码顶部
有人可以帮忙吗?这是代码。谢谢!iOS约束在编程设置后未更新
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.
答
这里的问题是,你不上你的部件host
,port
,user
,password
和connectButton
因此关闭translatesAutoresizingMaskIntoConstraints
对于这些,NSAutoresizingMaskLayoutConstraint
将会产生与您的约束相冲突。
例如,您可以插入此
for view in [host, port, user, password, connectButton] as [UIView] {
view.translatesAutoresizingMaskIntoConstraints = false
}
到您的viewDidLoad
方法,self.view.translatesAutoresizingMaskIntoConstraints = false
之后。
您也可以删除您尝试在最后考虑你的约束使你的代码看起来就像这样:
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
}
然后你会意识到,你可能要在增加一些宽度限制的(什么我想是)文本字段;)。
无论如何,如果您希望在Interface Builder中禁用translatesAutoresizingMaskIntoConstraints
,请参阅this answer on "Unexpected NSAutoresizingMaskLayoutConstraint adding UIView from nib to autolayout storyboard scene"。
它不容易找到所提供的数据的问题更好地使用UIViewAlertForUnsatisfiableConstraints上的符号断点自己调试它 – AnshaD
请参阅:http://www.knowstack.com/swift-nslayoutconstraint-programatically-sample-code/ –