一些 S.O.问题展示了一种自动布局技术,用于确定 View 满足其约束所需的最小尺寸:[header systemLayoutSizeFittingSize: UILayoutFittingCompressedSize]
制作前systemLayoutSizeFittingSize:
调用,我见过的所有示例都强制进行布局更新,如下所示:
[view setNeedsLayout];
[view layoutIfNeeded];
CGFloat height = [view systemLayoutSizeFittingSize: UILayoutFittingCompressedSize].height;
我想知道这实际上何时是必要的,因为它似乎是作为一种仪式调味料撒在其中的:我想了解为什么我要打电话,而不是为了运气!
我刚用过
systemLayoutSizeFittingSize:
在某些代码中,我有选择地更新 View 是 UITableView
实例的 tableViewHeader
(不是节标题),然后调整它的大小。如果没有额外的调用,它似乎可以正常工作。我的 viewDidLoad
中有这个:{
// Remove the view that we don't want.
[self.autoPopulateView removeFromSuperview];
// Resize the table's header view now the unwanted view is removed.
UIView *const header = self.tableView.tableHeaderView;
// Don't explicitly layout.
// [header setNeedsLayout];
// [header layoutIfNeeded];
CGFloat height = [header systemLayoutSizeFittingSize: UILayoutFittingCompressedSize].height;
CGRect frame = header.frame;
frame.size.height = height;
header.frame = frame;
}
谢谢。
最佳答案
在调用 systemLayoutSizeFittingSize 之前,绝对不需要进行这些调用。只要您的所有约束都到位,您就不需要做任何其他事情。
事实上,从性能的角度来看,预先强制布局可能是有害的,我认为这样做不仅没有必要,而且实际上是有害的。
关于uiview - systemLayoutSizeFittingSize : require setNeedsLayout & layoutIfNeeded before it?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28091760/