如何获取以UIStackView作为其子视图的UIView的internalContentSize?

时间:2019-07-02 18:13:38

标签: ios autolayout uistackview swift4.2

我将一个堆栈视图放置在一个视图内,并向该堆栈视图的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)

0 个答案:

没有答案