Reputation: 5816
Given the junk contentSize in UITableView on ios 11 how to I get a tableview with variable height cells to autosize to that content?
Answers here: Resizing UITableView to fit content have rotted and no longer apply to ios 11
Upvotes: 2
Views: 443
Reputation: 11435
It looks like when UITableView
is using Automatic Dimensions - UITableViewAutomaticDimension
, it calculates the contentSize
dynamically - as you scroll.
So the solution is to catch those changes using KVO:
var contentSizeObservation: NSKeyValueObservation?
override func viewDidLoad() {
super.viewDidLoad()
contentSizeObservation = tableView.observe(\.contentSize, options: [.new], changeHandler: { tableView, value in
// Do the setup here, this will be called multiple times
})
}
You need to define the contentSizeObservation
as global (as a class member), or else the changeHandler
block won't get called because the variable will get cleared by ARC.
Upvotes: 4