我想将一组自定义 View 水平放置在 UIStackView
中。
每个 subview 都有一个宽度和高度等于常量
约束:
let stack = UIStackView(frame: frameRect)
stack.axis = UILayoutConstraintAxis.horizontal
stack.distribution = UIStackViewDistribution.equalSpacing
stack.layoutMargins = UIEdgeInsets(top: 0, left: 40, bottom: 0, right: 40)
stack.isLayoutMarginsRelativeArrangement = true
for i in 1...3 {
let numberView = RotatingNumberView()
numberView.heightAnchor.constraint(equalToConstant: CGFloat(cellSize))
numberView.widthAnchor.constraint(equalToConstant: CGFloat(cellSize))
numberViews.append(numberView)
stack.addArrangedSubview(numberView)
}
如果我更改为 fillEqually
分布,则会显示 subview ,但它们的宽度会被拉伸(stretch)以填充 stackView。
我猜它与拥抱或压缩阻力优先级有关,但在 subview 中设置这些不起作用。
这里发生了什么?
最佳答案
对于除 fillEqually
之外的所有分布,堆栈 View 在计算其沿堆栈轴的大小时使用每个排列 View 的 intrinsicContentSize
属性。
因此覆盖此属性将起作用。
override var intrinsicContentSize: CGSize { return CGSize(your values) }
关于ios - 为什么当我将 UIStackView 分布设置为 equalspacing 时 subview 不可见?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44127089/