在Swift中,如何使用Layout Anchor设置多个子view的宽度相同(没有常量)?
In Swift, how to use Layout Anchor to set multiple subviews to have the same width (no constant)?
例如,我有一个包含 3 个子视图的父视图。我正在以编程方式使用 Layout Anchor 并尝试实现以下布局
|--subview 1--|--subview 2--|--subview 3--|
三个子视图中的每一个都具有相同的宽度。也就是说,
subView1.width = subView2.width = subView3.width
subView1.width+subView2.width+subView3.width = parentView.width
我知道我可以使用乘数来设置子视图 1 和子视图 2 的宽度:
subView1.widthAnchor.constraintEqualToAnchor(contentView.widthAnchor, multiplier: 1/3).active = true
subView2.widthAnchor.constraintEqualToAnchor(contentView.widthAnchor, multiplier: 1/3).active = true
并且 subView3 可以让前导锚点与 subView2 的尾随锚点对齐。
然而,我在某处看到Interface Builder实际上可以直接将这三个宽度分配为相等(无需使用常量或乘数)。是否可以通过编程方式对 Layout Anchor 执行相同的操作?如何?谢谢
这是完整的锚点集(我自己试过了,很管用):
// Create subView1
let subView1 = UIView()
subView1.backgroundColor = UIColor.redColor()
subView1.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(subView1)
// Create subView2
let subView2 = UIView()
subView2.backgroundColor = UIColor.blueColor()
subView2.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(subView2)
// Create subView3
let subView3 = UIView()
subView3.backgroundColor = UIColor.greenColor()
subView3.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(subView3)
subView1.leadingAnchor.constraintEqualToAnchor(contentView.leadingAnchor).active = true
subView1.topAnchor.constraintEqualToAnchor(contentView.topAnchor).active = true
subView1.bottomAnchor.constraintEqualToAnchor(contentView.bottomAnchor).active = true
subView2.topAnchor.constraintEqualToAnchor(contentView.topAnchor).active = true
subView2.bottomAnchor.constraintEqualToAnchor(contentView.bottomAnchor).active = true
subView3.topAnchor.constraintEqualToAnchor(contentView.topAnchor).active = true
subView3.trailingAnchor.constraintEqualToAnchor(contentView.trailingAnchor).active = true
subView3.bottomAnchor.constraintEqualToAnchor(contentView.bottomAnchor).active = true
subView1.trailingAnchor.constraintEqualToAnchor(subView2.leadingAnchor).active = true
subView2.trailingAnchor.constraintEqualToAnchor(subView3.leadingAnchor).active = true
// Add equal width anchors
subView1.widthAnchor.constraintEqualToAnchor(subView2.widthAnchor).active = true
subView2.widthAnchor.constraintEqualToAnchor(subView3.widthAnchor).active = true
希望这对您有所帮助。 :)
例如,我有一个包含 3 个子视图的父视图。我正在以编程方式使用 Layout Anchor 并尝试实现以下布局
|--subview 1--|--subview 2--|--subview 3--|
三个子视图中的每一个都具有相同的宽度。也就是说,
subView1.width = subView2.width = subView3.width subView1.width+subView2.width+subView3.width = parentView.width
我知道我可以使用乘数来设置子视图 1 和子视图 2 的宽度:
subView1.widthAnchor.constraintEqualToAnchor(contentView.widthAnchor, multiplier: 1/3).active = true
subView2.widthAnchor.constraintEqualToAnchor(contentView.widthAnchor, multiplier: 1/3).active = true
并且 subView3 可以让前导锚点与 subView2 的尾随锚点对齐。
然而,我在某处看到Interface Builder实际上可以直接将这三个宽度分配为相等(无需使用常量或乘数)。是否可以通过编程方式对 Layout Anchor 执行相同的操作?如何?谢谢
这是完整的锚点集(我自己试过了,很管用):
// Create subView1
let subView1 = UIView()
subView1.backgroundColor = UIColor.redColor()
subView1.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(subView1)
// Create subView2
let subView2 = UIView()
subView2.backgroundColor = UIColor.blueColor()
subView2.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(subView2)
// Create subView3
let subView3 = UIView()
subView3.backgroundColor = UIColor.greenColor()
subView3.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(subView3)
subView1.leadingAnchor.constraintEqualToAnchor(contentView.leadingAnchor).active = true
subView1.topAnchor.constraintEqualToAnchor(contentView.topAnchor).active = true
subView1.bottomAnchor.constraintEqualToAnchor(contentView.bottomAnchor).active = true
subView2.topAnchor.constraintEqualToAnchor(contentView.topAnchor).active = true
subView2.bottomAnchor.constraintEqualToAnchor(contentView.bottomAnchor).active = true
subView3.topAnchor.constraintEqualToAnchor(contentView.topAnchor).active = true
subView3.trailingAnchor.constraintEqualToAnchor(contentView.trailingAnchor).active = true
subView3.bottomAnchor.constraintEqualToAnchor(contentView.bottomAnchor).active = true
subView1.trailingAnchor.constraintEqualToAnchor(subView2.leadingAnchor).active = true
subView2.trailingAnchor.constraintEqualToAnchor(subView3.leadingAnchor).active = true
// Add equal width anchors
subView1.widthAnchor.constraintEqualToAnchor(subView2.widthAnchor).active = true
subView2.widthAnchor.constraintEqualToAnchor(subView3.widthAnchor).active = true
希望这对您有所帮助。 :)