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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使自动布局约束依赖于多个其他锚点?相关的知识,希望对你有一定的参考价值。

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

例如:

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

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

答案

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

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的高度。

以上是关于如何使自动布局约束依赖于多个其他锚点?的主要内容,如果未能解决你的问题,请参考以下文章

使用自动布局时,如何调整 CALayer 的锚点?

斯威夫特:我无法使自动布局约束适用于我的 UIScrollView

自动布局 - 使视图的尺寸依赖于另一个视图的

如何使用自动布局使超级视图适合最大的内在大小子视图

iOS - 具有自动布局的依赖约束控制台输出

如何使唯一数组的自定义验证规则依赖于其他字段 laravel