视图的底部和右侧约束是否定的?
Bottom and Right constraints of view are negative?
这是一种将子视图固定到 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,这会产生所需的结果,但我不必这样做?我哪里错了?
非常感谢。
听起来很疯狂。现在无法查看,但我倾向于在情节提要中设置约束,为它们创建出口并像这样更改它们:
self.menuTopConstraint.constant = 64
会将菜单的顶部约束向下移动到导航栏的底部。
将我的主视图向下滑动菜单的高度:
self.mainViewBottomConstraint.constant = self.indexHeight * -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
比 right/bottom 比 view
.
这是一种将子视图固定到 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,这会产生所需的结果,但我不必这样做?我哪里错了?
非常感谢。
听起来很疯狂。现在无法查看,但我倾向于在情节提要中设置约束,为它们创建出口并像这样更改它们:
self.menuTopConstraint.constant = 64
会将菜单的顶部约束向下移动到导航栏的底部。
将我的主视图向下滑动菜单的高度:
self.mainViewBottomConstraint.constant = self.indexHeight * -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
比 right/bottom 比 view
.