I have UITableView
with UITableViewAutomaticDimension
and some estimatedRowHeight
. For this table I am using custom UITableViewCell
which contains some label and custom UIView
with overridden intrinsicContentSize()
. Constraints setup is correct and table is able to determine actual height for each row. So far so good.
Now I started to modify internal logic of my custom view to adapt it's appearance based on available width i.e. when table cell size is not wide enough my view can rearrange subviews to fit new limitation and this have impact to resulting height, so I have code like that:
var internalSize: CGSize = ...
override func intrinsicContentSize() -> CGSize {
return internalSize
}
override func layoutSubviews() {
super.layoutSubviews()
fitIntoWidth(frame.size.width)
}
private func fitIntoWidth(width: CGFloat) {
let height = // calculate based on content and width
internalSize = CGSizeMake(width, height)
invalidateIntrinsicContentSize()
}
Now, when I populate table view, intrinsicContentSize()
returns some desired value but it is not good fit for current layout, then control goes to layoutSubviews()
where size get recalculated and system again calls intrinsicContentSize()
and now it returns good value. However, first time table loads data and cell heights calculated based on incorrect intrinsicContentSize()
values. If I call reloadData()
again all becomes fine and layout is also ok for all upcoming cells in table.
Where is my mistake and how to modify code to make cell sizing work correctly without calling reloadData()
twice?
UILabel
- you just set it's text and never height - label itself calculates desired size – ChivalricsystemLayoutSizeFittingSize
not get called at all. And docs also mentions intrinsic content size – Chivalric