UITableViewCell 基于百分比的高度和最小高度

UITableViewCell Percentage Based Height with Minimum Height

我有一个自定义的表格视图单元格,我让自动布局管理它的高度。

tableView.estimatedRowHeight = 44 
tableView.rowHeight = UITableViewAutomaticDimension

在 TableViewCell 中我有一个子视图:

contentView.addSubview(subView)

我希望 TableViewCell 为屏幕高度的 40% 或 250pts。

我已经通过这样做让它工作了:

self.contentView.autoSetDimension(.Height, toSize: round(UIScreen.mainScreen().bounds.height * 0.40), relation: .GreaterThanOrEqual)
self.contentView.autoPinEdgeToSuperviewEdge(.Leading)
self.contentView.autoPinEdgeToSuperviewEdge(.Trailing)
self.subView.autoSetDimension(.Height, toSize: 250, relation: .GreaterThanOrEqual)

subView.autoPinEdgeToSuperviewEdge(.Top)
subView.autoPinEdgeToSuperviewEdge(.Leading)
subView.autoPinEdgeToSuperviewEdge(.Trailing)
subView.autoPinEdgeToSuperviewEdge(.Bottom)

我觉得这有点草率。这是最好的方法吗?

您不需要向 contentView 添加约束,它将自动调整其大小以适应 subView,试试这个:

subView.autoSetDimension(.Height, toSize: round(UIScreen.mainScreen().bounds.height * 0.40), relation: .GreaterThanOrEqual)
subView.autoSetDimension(.Height, toSize: 250, relation: .GreaterThanOrEqual)

subView.autoPinEdgesToSuperviewEdgesWithInsets(UIEdgeInsetsZero)