Auto-layout margins with anchors

别来无恙 提交于 2019-12-09 19:10:22

问题


So, I thought the following was equivalent?

# 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

Though I appear to be wrong. How would I get the 5 margin between the containerView and the parent using margins, constraints and anchors?


回答1:


I found a solution by reading this question: Auto Layout: layoutMarginsGuide.

There seems to be an issue when setting layoutMargins from the init.

Though, instead of setting layoutMargins in viewDidMoveToSuperView I put it in updateConstraints instead, which also worked fine.

So the code is now:

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)
}

which then had the same effect as:

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



回答2:


It seems that setting layoutMargins in updateConstraints() works, but with the following caveat:

override func updateConstraints() {
    super.updateConstraints()
    layoutMargins = UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8)
}

The above code would fail to update the margins because the new value is the same as the system default. So to be safe, first reset the margins to zero:

override func updateConstraints() {
    super.updateConstraints()
    layoutMargins = .zero
    layoutMargins = UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8)
}

Also, if your goal is to set the initial margins to .zero, it's probably a good idea to first set them to a non-zero value, just to make sure the view is notified of the change.




回答3:


Objective-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;

Swift 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


来源:https://stackoverflow.com/questions/40423079/auto-layout-margins-with-anchors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!