当height给出时,如何通过systemLayoutSizeFittingSize获取内容正确的视图宽度

时间:2016-10-27 14:49:02

标签: ios width

我想用autolayout计算视图大小,但它无法获得正确的值。这是我的代码

// build views
let view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
let posterView = UIImageView(frame: CGRect(x: 0, y: 0, width: 50, height: 100))
let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 20))

 titleLabel.text = "Title  "
 titleLabel.font = UIFont.systemFontOfSize(16) // label height = 19.5

 view.addSubview(posterView)
 view.addSubview(titleLabel)

 posterView.snp_makeConstraints { (make) in
     make.top.left.right.equalTo(view).offset(0)
     make.width.equalTo(posterView.snp_height).multipliedBy(16.0 / 9.0) 
 }

 titleLabel.snp_makeConstraints { (make) in
    make.top.equalTo(posterView.snp_bottom).offset(0)
    make.left.equalTo(view).offset(0).priority(750)
    make.right.equalTo(view).offset(0).priority(750)

    make.bottom.equalTo(view.snp_bottom).offset(0)
 }

 // calculate 
 view.translatesAutoresizingMaskIntoConstraints = false

 let heightConstraint = NSLayoutConstraint(item: view, height: 90+19.5)
 view.addConstraint(heightConstraint)

 let fittingSize = view.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize) 
 // the fittingSize is (40,109.5), not expect value (160, 109.5)

请不要告诉我如何计算UITableViewCell的动态高度。这是从高到宽的相反问题。

由于

0 个答案:

没有答案