如何使自动布局约束依赖于多个其他锚点?

时间:2018-03-02 00:31:12

标签: ios autolayout nslayoutconstraint

如何使用自动布局使视图的高度等于其他两个视图高度的总和?

例如:

viewA.heightAnchor.constraint(equalTo: ...),
viewB.heightAnchor.constraint(equalTo: ...),
viewC.heightAnchor.constraint(equalTo: viewA.heightAnchor + viewB.heightAnchor)

是否有解决方案不涉及设置常量值并在每个视图边界更改时重新计算它?

1 个答案:

答案 0 :(得分:2)

可以,但我认为只有在一些帮助者视图的帮助下。看到这个示例我刚刚开始实现的游乐场:

import Foundation
import UIKit
import PlaygroundSupport

let vc = UIViewController()
vc.view = UIView()
vc.view.backgroundColor = .white

let viewA = UIView()
let viewB = UIView()
let viewC = UIView()

viewA.backgroundColor = .red
viewB.backgroundColor = .green
viewC.backgroundColor = .blue

viewA.translatesAutoresizingMaskIntoConstraints = false
viewB.translatesAutoresizingMaskIntoConstraints = false
viewC.translatesAutoresizingMaskIntoConstraints = false

vc.view.addSubview(viewA)
vc.view.addSubview(viewB)
vc.view.addSubview(viewC)

let helperViewA = UIView()
let helperViewB = UIView()

helperViewA.translatesAutoresizingMaskIntoConstraints = false
helperViewB.translatesAutoresizingMaskIntoConstraints = false

helperViewA.isHidden = true
helperViewB.isHidden = true

vc.view.addSubview(helperViewA)
vc.view.addSubview(helperViewB)

viewA.bottomAnchor.constraint(equalTo: vc.view.bottomAnchor).isActive = true
viewA.leadingAnchor.constraint(equalTo: vc.view.leadingAnchor).isActive = true
viewA.trailingAnchor.constraint(equalTo: viewB.leadingAnchor).isActive = true
viewA.heightAnchor.constraint(equalToConstant: 20.0).isActive = true

viewB.bottomAnchor.constraint(equalTo: vc.view.bottomAnchor).isActive = true
viewB.widthAnchor.constraint(equalTo: viewA.widthAnchor, multiplier: 1.0).isActive = true
viewB.trailingAnchor.constraint(equalTo: vc.view.trailingAnchor).isActive = true
viewB.heightAnchor.constraint(equalToConstant: 40.0).isActive = true

viewA.heightAnchor.constraint(equalTo: helperViewA.heightAnchor, multiplier: 1.0).isActive = true
viewB.heightAnchor.constraint(equalTo: helperViewB.heightAnchor, multiplier: 1.0).isActive = true

helperViewA.bottomAnchor.constraint(equalTo: helperViewB.topAnchor).isActive = true

viewC.widthAnchor.constraint(equalToConstant: 100).isActive = true
viewC.topAnchor.constraint(equalTo: helperViewA.topAnchor).isActive = true
viewC.bottomAnchor.constraint(equalTo: helperViewB.bottomAnchor).isActive = true


helperViewA.leadingAnchor.constraint(equalTo: viewC.leadingAnchor).isActive = true
helperViewA.trailingAnchor.constraint(equalTo: viewC.trailingAnchor).isActive = true
helperViewB.leadingAnchor.constraint(equalTo: viewC.leadingAnchor).isActive = true
helperViewB.trailingAnchor.constraint(equalTo: viewC.trailingAnchor).isActive = true

viewC.centerXAnchor.constraint(equalTo: vc.view.centerXAnchor).isActive = true
viewC.centerYAnchor.constraint(equalTo: vc.view.centerYAnchor).isActive = true

vc.view.frame.size = CGSize(width: 375, height: 667)
PlaygroundPage.current.liveView = vc.view

我们的想法是,您有两个垂直堆叠的辅助视图,由顶部的bottomAnchor和底部的topAnchor连接。然后,您可以设置每个帮助程序视图'高度等于viewAviewB高度。然后,您的viewC可以附加到顶部视图的topAnchor和底部视图的bottomAnchor,其中viewC的结果是高度viewA的高度加上viewB的高度。