我有一个简单的类,它在堆栈中包含两个标签和一行:

class TestView: UIView
{
    let label_A     = UILabel()
    let label_B     = UILabel()

    override init(frame: CGRect)            { super.init(frame: frame);     setup() }
    required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder);  setup() }
    func setup()
    {
        let view_BG     = UIView()
        let view_LineH  = UIView()

        // Configure
        view_BG.backgroundColor = .white
        view_BG.layer.cornerRadius = 6

        view_LineH.backgroundColor = .gray

        label_A.numberOfLines = 0
        label_A.backgroundColor = UIColor(red: 1, green: 0, blue: 0, alpha: 0.2)
        label_A.textColor = .red
        label_B.numberOfLines = 0
        label_B.textColor = .blue
        label_B.backgroundColor = UIColor(red: 0, green: 0, blue: 1, alpha: 0.2)

        label_A.text = "TestA"
        label_B.text = "Experiment with Swift standard library types and learn high-level concepts using visualizations and practical examples. Learn how the Swift standard library uses protocols and generics to express powerful constraints. Download the playground below to get started."

        // Assemble
        self.addSubview(view_BG)
        view_BG.addSubview(label_A)
        view_BG.addSubview(view_LineH)
        view_BG.addSubview(label_B)

        // Layout
        view_BG.constrain_edges(to: self)
        label_A.constrain_edges(to: view_BG, excludingEdge: .bottom)
        label_B.constrain_edges(to: view_BG, excludingEdge: .top)

        view_LineH.constrain_height(1)
        view_LineH.constrain_left(to: view_BG)
        view_LineH.constrain_right(to: view_BG)
        view_LineH.constrain_topToBottom(of: label_A)
        label_B.constrain_topToBottom(of: view_LineH)
    }
}


当我打电话给sizeThatFits时,它只是向我吐出身高:

let v = TestView()
let s = v.sizeThatFits(CGSize(width: 200, height: 10000))
// s is (200, 10000)


如何计算给定宽度下的所需高度?

最佳答案

我相信你想要.systemLayoutSizeFitting()

    let tv = TestView()

    let targSize = CGSize(width: 200, height: 10000)

    let fitSize = tv.systemLayoutSizeFitting(targSize, withHorizontalFittingPriority: 1000, verticalFittingPriority: 1)

    print(fitSize)

    // prints "(200.0, 245.0)" in my playground


我没有任何用于.constrain_edges的东西,所以这是我正在运行的实际视图类:

class TestView: UIView
{
    let label_A     = UILabel()
    let label_B     = UILabel()

    override init(frame: CGRect)            { super.init(frame: frame);     setup() }
    required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder);  setup() }
    func setup()
    {
        let view_BG     = UIView()
        let view_LineH  = UIView()

        // Configure
        view_BG.backgroundColor = .white
        view_BG.layer.cornerRadius = 6

        view_LineH.backgroundColor = .gray

        label_A.numberOfLines = 0
        label_A.backgroundColor = UIColor(red: 1, green: 0, blue: 0, alpha: 0.2)
        label_A.textColor = .red
        label_B.numberOfLines = 0
        label_B.textColor = .blue
        label_B.backgroundColor = UIColor(red: 0, green: 0, blue: 1, alpha: 0.2)

        label_A.text = "TestA"
        label_B.text = "Experiment with Swift standard library types and learn high-level concepts using visualizations and practical examples. Learn how the Swift standard library uses protocols and generics to express powerful constraints. Download the playground below to get started."

        // Assemble
        self.addSubview(view_BG)
        view_BG.addSubview(label_A)
        view_BG.addSubview(view_LineH)
        view_BG.addSubview(label_B)

        view_BG.translatesAutoresizingMaskIntoConstraints = false
        label_A.translatesAutoresizingMaskIntoConstraints = false
        label_B.translatesAutoresizingMaskIntoConstraints = false
        view_LineH.translatesAutoresizingMaskIntoConstraints = false

        // Layout
        view_BG.topAnchor.constraint(equalTo: self.topAnchor, constant: 0.0).isActive = true
        view_BG.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 0.0).isActive = true
        view_BG.rightAnchor.constraint(equalTo: self.rightAnchor, constant: 0.0).isActive = true
        view_BG.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 0.0).isActive = true


        label_A.topAnchor.constraint(equalTo: view_BG.topAnchor, constant: 0.0).isActive = true
        label_A.leftAnchor.constraint(equalTo: view_BG.leftAnchor, constant: 0.0).isActive = true
        label_A.rightAnchor.constraint(equalTo: view_BG.rightAnchor, constant: 0.0).isActive = true

        label_B.leftAnchor.constraint(equalTo: view_BG.leftAnchor, constant: 0.0).isActive = true
        label_B.rightAnchor.constraint(equalTo: view_BG.rightAnchor, constant: 0.0).isActive = true
        label_B.bottomAnchor.constraint(equalTo: view_BG.bottomAnchor, constant: 0.0).isActive = true

        view_LineH.leftAnchor.constraint(equalTo: view_BG.leftAnchor, constant: 0.0).isActive = true
        view_LineH.rightAnchor.constraint(equalTo: view_BG.rightAnchor, constant: 0.0).isActive = true

        view_LineH.topAnchor.constraint(equalTo: label_A.bottomAnchor, constant: 0.0).isActive = true
        label_B.topAnchor.constraint(equalTo: view_LineH.bottomAnchor, constant: 0.0).isActive = true

        view_LineH.heightAnchor.constraint(equalToConstant: 1.0).isActive = true

    }
}

关于ios - sizeThatFits为什么不能与UILabel一起使用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46453831/

10-09 06:31