那么,我认为以下内容是等效的?
# This is how I usually do
contentView.leftAnchor.constraint(equalTo: leftAnchor, constant: 5).isActive = true
contentView.topAnchor.constraint(equalTo: topAnchor, constant: 5).isActive = true
contentView.rightAnchor.constraint(equalTo: rightAnchor, constant: -5).isActive = true
contentView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -5).isActive = true
# This is what I tried. I expected the same result..
layoutMargins = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
contentView.leftAnchor.constraint(equalTo: layoutMarginsGuide.leftAnchor).isActive = true
contentView.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor).isActive = true
contentView.rightAnchor.constraint(equalTo: layoutMarginsGuide.rightAnchor).isActive = true
contentView.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor).isActive = true
虽然我看似错了。如何使用边距,约束和锚点获得containerView和父级之间的5个边距?
答案 0 :(得分:3)
我通过阅读这个问题找到了解决方案:Auto Layout: layoutMarginsGuide。
从layoutMargins
设置init
时似乎存在问题。
虽然我没有在layoutMargins
中设置viewDidMoveToSuperView
,而是将其放在updateConstraints
中,而且工作正常。
所以代码现在是:
override init(frame: CGRect) {
super.init(frame: frame)
contentView.leftAnchor.constraint(equalTo: layoutMarginsGuide.leftAnchor).isActive = true
contentView.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor).isActive = true
contentView.rightAnchor.constraint(equalTo: layoutMarginsGuide.rightAnchor).isActive = true
contentView.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor).isActive = true
}
override func updateConstraints() {
super.updateConstraints()
layoutMargins = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
}
然后具有与以下相同的效果:
contentView.leftAnchor.constraint(equalTo: leftAnchor, constant: 5).isActive = true
contentView.topAnchor.constraint(equalTo: topAnchor, constant: 5).isActive = true
contentView.rightAnchor.constraint(equalTo: rightAnchor, constant: -5).isActive = true
contentView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -5).isActive = true
答案 1 :(得分:0)
似乎在layoutMargins
中设置updateConstraints()
有效,但有以下警告:
override func updateConstraints() {
super.updateConstraints()
layoutMargins = UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8)
}
上述代码无法更新边距,因为新值与系统默认值相同。所以为了安全起见,首先将边距重置为零:
override func updateConstraints() {
super.updateConstraints()
layoutMargins = .zero
layoutMargins = UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8)
}
此外,如果您的目标是将初始边距设置为.zero
,那么首先将它们设置为非零值可能是个好主意,只是为了确保视图会通知更改。 / p>
答案 2 :(得分:-1)
目标-C:
[contentView.leftAnchor constraintEqualToAnchor:contentView.superview.leftAnchor constant:5].active = YES;
[contentView.rightAnchor constraintEqualToAnchor:contentView.superview.rightAnchor constant:5].active = YES;
[contentView.topAnchor constraintEqualToAnchor:contentView.superview.topAnchor constant:5].active = YES;
[contentView.bottomAnchor constraintEqualToAnchor:contentView.superview.bottomAnchor constant:5].active = YES;
斯威夫特3:
contentView.leftAnchor.constraint(equalTo: contentView.superview?.leftAnchor, constant: 5).isActive = true
contentView.rightAnchor.constraint(equalTo: contentView.superview?.rightAnchor, constant: 5).isActive = true
contentView.topAnchor.constraint(equalTo: contentView.superview?.topAnchor, constant: 5).isActive = true
contentView.bottomAnchor.constraint(equalTo: contentView.superview?.bottomAnchor, constant: 5).isActive = true