我将一个堆栈视图放置在一个视图内,并向该堆栈视图的Leading-Trailing-Top-Bottom边缘添加了约束。现在,我如何获得intrinsicContentSize
中的UIView
class CustomView: UIView {
override func layoutSubviews() {
super.layoutSubviews()
containerStack?.topAnchor.constraint(equalTo: topAnchor, constant: 24).isActive = true
containerStack?.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -24).isActive = true
containerStack?.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 20).isActive = true
containerStack?.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -20).isActive = true
}
//MARK: Private Methods
private func setupViews() {
setupContainerStackView()
setupTitleLabel()
}
private func setupContainerStackView() {
containerStack = UIStackView()
containerStack?.axis = .vertical
containerStack?.spacing = 24.0
containerStack?.alignment = .fill
containerStack?.distribution = .fill
containerStack?.translatesAutoresizingMaskIntoConstraints = false
addSubview(containerStack!)
}
private func setupTitleLabel() {
titleLabel = UILabel()
titleLabel?.numberOfLines = 0
titleLabel?.text = title
titleLabel?.textAlignment = .left
titleLabel?.translatesAutoresizingMaskIntoConstraints = false
containerStack?.addArrangedSubview(titleLabel!)
}
}
当我尝试访问intrinsicContentSize
时,它总是返回(-1,-1)