Swift 另一个UIStackView问题中的UIStackView和占位符视图

Swift 另一个UIStackView问题中的UIStackView和占位符视图,swift,uikit,uistackview,Swift,Uikit,Uistackview,如果在另一个UIStackView(mainStack)中有UIStackView(testStack)和占位符UIView(testView),则会出现问题。这意味着如果testStack中没有内容,它将崩溃,testView将占用所有空间。对于testStack甚至有一个content-hugging priority设置为maximum,因此当没有子视图时,它应该将其高度折叠为0。但事实并非如此。当没有内容时,如何使其崩溃 PS如果testStack中有项目,则一切都会按预期工作:test

如果在另一个
UIStackView(mainStack)
中有
UIStackView(testStack)
和占位符
UIView(testView)
,则会出现问题。这意味着如果
testStack
中没有内容,它将崩溃,
testView
将占用所有空间。对于
testStack
甚至有一个
content-hugging priority
设置为maximum,因此当没有子视图时,它应该将其高度折叠为0。但事实并非如此。当没有内容时,如何使其崩溃

PS如果
testStack
中有项目,则一切都会按预期工作:
testView
占用所有可用空间,
testStack
只占用空间以适合其子视图

class AView: UIView {
    lazy var mainStack: UIStackView = {
        let stack = UIStackView()
        stack.axis = .vertical
        stack.backgroundColor = .gray
        stack.addArrangedSubview(self.testStack)
        stack.addArrangedSubview(self.testView)
        return stack
    }()
    
    let testStack: UIStackView = {
        let stack = UIStackView()
        stack.backgroundColor = .blue
        stack.setContentHuggingPriority(.init(1000), for: .vertical)
        return stack
    }()
    
    let testView: UIView = {
        let view = UIView()
        view.backgroundColor = .red
        return view
    }()
    
    init() {
        super.init(frame: .zero)
        backgroundColor = .yellow
        addSubview(mainStack)
        mainStack.translatesAutoresizingMaskIntoConstraints = false
        mainStack.topAnchor.constraint(equalTo: topAnchor).isActive = true
        mainStack.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
        mainStack.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
        mainStack.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

当自动布局在
UIStackView
中排列子视图时,它会查看:

  • 堆栈视图的
    .distribution
    属性
  • 子视图的高度约束(如果给定)
  • 子视图的固有内容大小
由于您尚未指定
.distribution
属性,
mainStack
正在使用默认值
.fill

UIStackView
具有固有内容大小,因此自动布局显示“testStack的高度为零”

UIView
具有固有内容大小,因此自动布局显示“testView的高度为零”

由于分布为“填充”,因此“自动布局”实际上会说:“排列的子视图的高度不明确,因此让我们将最后一个子视图的高度设为零,并用第一个子视图填充主视图

设置
.setContentHuggingPriority
将无效,因为“拥抱”没有固有的高度

如果设置mainStack的
.distribution=.fillEqualified
,您将得到(如预期的那样)
testStack
填充上半部分,而
testView
填充下半部分

如果设置mainStack的
.distribution=.fillProportionally
,将得到相同的结果…
testStack
填充上半部分,而
testView
填充下半部分,因为
.fillProportionally
使用排列的子视图的固有内容大小…在这种情况下,它们都是零,所以“比例“将是平等的

如果设置主堆栈的
.distribution=.equalspace
.distribution=.equalCentering
,则不会看到
testStack
testView
…自动布局将为每个主堆栈的高度设置为零,并用(空)“间距”填充剩余的
mainStack

如果您的目标是让
testStack
在它为空时“消失”,您可以:

  • 将其设置为隐藏,或
  • 将其子类化,并赋予其固有高度

当自动布局在
UIStackView
中排列子视图时,它会查看:

  • 堆栈视图的
    .distribution
    属性
  • 子视图的高度约束(如果给定)
  • 子视图的固有内容大小
由于您尚未指定
.distribution
属性,
mainStack
正在使用默认值
.fill

UIStackView
具有固有内容大小,因此自动布局显示“testStack的高度为零”

UIView
具有固有内容大小,因此自动布局显示“testView的高度为零”

由于分布为“填充”,因此“自动布局”实际上会说:“排列的子视图的高度不明确,因此让我们将最后一个子视图的高度设为零,并用第一个子视图填充主视图

设置
.setContentHuggingPriority
将无效,因为“拥抱”没有固有的高度

如果设置mainStack的
.distribution=.fillEqualified
,您将得到(如预期的那样)
testStack
填充上半部分,而
testView
填充下半部分

如果您按比例设置mainStack的
.distribution=.fill
,您将得到相同的结果
testStack
填充上半部分,而
testView
填充下半部分,因为
.Fill按比例使用排列的子视图的固有内容大小。。。在这种情况下,它们都是零,所以“比例”相等

如果设置mainStack的
.distribution=.equalspace
.distribution=.equalCentering
,则不会看到
testStack
testView
。。。“自动布局”将使每个组件的高度为零,并用(空)“间距”填充其余的
mainStack

如果您的目标是让
testStack
在它为空时“消失”,您可以:

  • 将其设置为隐藏,或
  • 将其子类化,并赋予其固有高度

在主视图中添加
AView
的实例时,是否对其进行高度约束?@DonMag我将其设置为superview的边缘,这在本质上为其提供了层次结构在主视图中添加
AView
的实例时,你给它一个高度限制吗?@DonMag我让它成为superview的边缘,这实际上给了它高度限制