【问题标题】:Bottom and Right constraints of view are negative?视图的底部和右侧约束是否为负?
【发布时间】:2017-03-24 09:37:20
【问题描述】:

这是我将子视图固定到UIView 实例的方法。抱歉代码乱七八糟,我一直在尝试让它工作。

open static func withEmbeddedView(_ view: UIView, andFixedHeight height: CGFloat) -> PMGAlertControllerEmbedComponent {
    let base = PMGAlertControllerEmbedComponent(frame: CGRect.zero)
    base.addSubview(view)
    view.translatesAutoresizingMaskIntoConstraints = false
    base.translatesAutoresizingMaskIntoConstraints = false

    let left = NSLayoutConstraint(item: view, attribute: .left, relatedBy: .equal, toItem: base, attribute: .left, multiplier: 1, constant: 0)
    let right = NSLayoutConstraint(item: view, attribute: .right, relatedBy: .equal, toItem: base, attribute: .right, multiplier: 1, constant: 0)
    let top = NSLayoutConstraint(item: view, attribute: .top, relatedBy: .equal, toItem: base, attribute: .top, multiplier: 1, constant: 0)
    let bottom = NSLayoutConstraint(item: view, attribute: .bottom, relatedBy: .equal, toItem: base, attribute: .bottom, multiplier: 1, constant: 0)
    base.addConstraints([left, right, top, bottom])
    view.addConstraint(NSLayoutConstraint(item: view, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: height))
    base.leftConstraint = left
    base.rightConstraint = right
    base.topConstraint = top
    base.bottomConstraint = bottom
    base.layoutIfNeeded()
    return base
}

现在这本身可以正常工作,但是一旦我尝试在其他地方修改这些约束的常量,比如将它们设置为 16 以添加一些填充,右侧和底部约束就会相反!所以不是子视图比父视图小 16pts,而是比父视图大 16pts?顶部和左侧的行为符合预期。

请注意,如果我将顶部和左侧设置为 16,但底部和右侧设置为 -16,这会产生所需的结果,但我不应该这样做吗?我哪里错了?

非常感谢。

【问题讨论】:

    标签: ios swift uiview nslayoutconstraint


    【解决方案1】:

    约束中项目的顺序很重要。

    改变

    let bottom = NSLayoutConstraint(item: view, attribute: .bottom, relatedBy: .equal, toItem: base, attribute: .bottom, multiplier: 1, constant: 0)
    let right = NSLayoutConstraint(item: view, attribute: .right, relatedBy: .equal, toItem: base, attribute: .right, multiplier: 1, constant: 0)
    

    let bottom = NSLayoutConstraint(item: base, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1, constant: 0)
    let right = NSLayoutConstraint(item: base, attribute: .right, relatedBy: .equal, toItem: view, attribute: .right, multiplier: 1, constant: 0)
    

    试着这样想:把第二个项目当作原点。如果值为正,则第一项在第二项的右/下方向。如果该值为负数,则表示方向是向左向上到第二个项目。

    由于您想将view 放入base 中,并且您希望约束中的值为正值,因此您需要使用base 作为第一项,并将view 作为第二项(原点)在约束中。因此,当约束的常数为正时,这意味着base 位于view 的右侧/底部。

    【讨论】:

    • 完全合乎逻辑现在我以这种方式思考它,感谢您提供可靠的解释和示例。 :)
    【解决方案2】:

    听起来很疯狂。现在无法查看,但我倾向于在情节提要中设置约束,为它们创建出口并更改它们,如下所示:

    self.menuTopConstraint.constant = 64
    

    会将菜单的顶部约束向下移动到导航栏的底部。

    将我的主视图向下滑动菜单的高度:

    self.mainViewBottomConstraint.constant = self.indexHeight * -1
    

    欣赏它并不能直接解决问题。有空的时候可以看看。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-24
      相关资源
      最近更新 更多