Does systemLayoutSizeFittingSize: does setNeedsLayout & layoutIfNeeded be required before this?

A number of SO questions show that the autodetection method to determine the minimum size required for a view requires its limitations to be met: [header systemLayoutSizeFittingSize: UILayoutFittingCompressedSize]

Before making a call to systemLayoutSizeFittingSize: all the examples I saw force a layout update, for example:

 [view setNeedsLayout]; [view layoutIfNeeded]; CGFloat height = [view systemLayoutSizeFittingSize: UILayoutFittingCompressedSize].height; 

I would like to know when this is actually necessary, because it seems to be sprinkled like a ritual seasoning: I would like to understand why I'm calling, and not doing it for good luck!

I just used systemLayoutSizeFittingSize: in some code, where I selectively update the view, which is a UITableView instance tableViewHeader (not the section title), and then resize it. It seems to be working fine without additional calls. I have this in my viewDidLoad :

 { // Remove the view that we don't want. [self.autoPopulateView removeFromSuperview]; // Resize the table 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; } 

Thanks.

+5
source share
2 answers

It is definitely not necessary to make one of these calls before calling systemLayoutSizeFittingSize. As long as all your restrictions are in place, you do not need to do anything.

In fact, forcing the transfer of the layout in advance is potentially harmful in terms of performance, and I would say that it is not only optional, but actually harmful.

0
source

it definitely seems that layoutIfNeeded is a responsive response to viewing inconsistencies in height calculations when using systemLayoutSizeFittingSize - the problem is that the value is very difficult to debug when it is incorrect

In my experience, you need to call layoutIfNeeded when your target view has updated the constraints in the code, for example, changing a constant, adding, removing a constraint. Calling setNeedsLayout or setNeedsUpdateConstraints does not work (shrug)

0
source

Source: https://habr.com/ru/post/1211705/


All Articles