ios - 为什么 sizeThatFits 不能与 UILabel 一起使用?

标签 ios swift uiview autolayout uilabel

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

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 的任何东西,所以这是我正在运行的实际 View 类:

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/

相关文章:

swift/Cloudkit : Handling user-changes while pulling

swift - 为什么这些 CAShapeLayers 没有到达预期的位置?

iphone - iOS - 将自定义 UIView 放入 View Controller 中

ios - 取消对 View 的多次触摸

ios - 过渡到导航 View Controller

ios - 在 iOS 中如何像 Java 中的 System.currentTimeMillis() 那样获取当前时间?

ios - 如何在 UITableViewRowAction 中添加图像?

ios - 属性 'self.delegate' 未在 super.init 中初始化

IOS - Android 的 Smack API 库可以用于 IOS 连接到 Ejabberd 服务器吗?

ios - Restkit:处理动态嵌套属性只返回一个对象