我在UIStackView中安排子视图的布局遇到了问题,并且想知道是否有人可以帮我理解发生了什么.
所以我有一些间距的UIStackView(例如1,但这没关系)和.fillProportionally分布.我添加的排列子视图只有1x1的intrinsicContentSize(可能是任何东西,只是方形视图),我需要在stackView中按比例拉伸它们.
问题是,如果我添加没有实际框架的视图,只有内在大小,那么我得到这个错误的布局
否则,如果我使用相同大小的帧添加视图,一切都按预期工作,
但我真的不想设置视图的框架.
我很确定这是关于拥抱和压缩阻力优先的全部,但无法弄清楚正确答案是什么.
这是一个游乐场示例:
import UIKit
import PlaygroundSupport
class LView: UIView {
// If comment this and leave only intrinsicContentSize - result is wrong
convenience init() {
self.init(frame: CGRect(x: 0, y: 0, width: 1, height: 1))
}
// If comment this and leave only convenience init(), then everything works as expected
public override var intrinsicContentSize: CGSize {
return CGSize(width: 1, height: 1)
}
}
let container = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
container.backgroundColor …Run Code Online (Sandbox Code Playgroud)