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

问题描述:

我有一个包含两个标签,并在堆栈行一个简单的类:为什么sizeThatFits不能与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) 

如何计算具有给定宽度的期望高度?

+0

它使感觉它会给你回来。 'TestView'中的所有约束都受限于边缘。但是你还没有设置'TestView'的框架。所以给定一个未知的框架大小的内部,你如何确定正确的帧大小来包含所有这些?你不能。 – TNguyen

+0

那么在sizeThatFits中指定* width *和a * height *有什么意义?如果我只是指定一个,那就不明确了。即使我在sizeThatFits调用之前添加了如下内容:v.frame = CGRect(x:0,y:0,width:columnWidth,height:100),结果也是一样的。 – GoldenJoe

我相信你想.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 

    } 
} 
+0

这很奇怪,它给我(0,285.5)。为了使它编译,我必须将参数设置为: let fitSize = c.systemLayoutSizeFitting(targetSize,withHorizo​​ntalFittingPriority:.required,verticalFittingPriority:.defaultLow) – GoldenJoe

+0

不知道是否与约束有关,但我添加了实际我正在运行这个测试。 – DonMag

+0

嗯....可能版本差异?我在Xcode 8.2.1操场上运行这个。如果你使用Xcode 9/Swift 4,那可能很重要。 – DonMag