【问题标题】:Auto-layout margins with anchors带有锚点的自动布局边距
【发布时间】:2017-03-18 07:28:31
【问题描述】:

那么,我认为以下是等价的?

# 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 边距?

【问题讨论】:

    标签: ios cocoa-touch autolayout nslayoutconstraint


    【解决方案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
    

    【讨论】:

    • 如果你阅读了这个问题,你会发现这正是你所说的我现在正在做的事情,我想知道如何让它与 layoutMarginGuides 一起工作
    【解决方案2】:

    我通过阅读这个问题找到了解决方案:Auto Layout: layoutMarginsGuide

    init 设置layoutMargins 时似乎存在问题。

    不过,我没有将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
    

    【讨论】:

    • 不确定在这种情况下是否重要,但一般情况下,应该在实现结束时调用super.updateConstraints()Apple Documentation
    【解决方案3】:

    似乎在updateConstraints() 中设置layoutMargins 有效,但有以下警告:

    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,最好先将它们设置为非零值,以确保通知视图更改。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-21
      • 1970-01-01
      • 1970-01-01
      • 2016-10-14
      相关资源
      最近更新 更多