Hi again,
I think the solution suggested above would probably work for a custom cell; I haven't tried it yet!
I came up with another possible solution for use with static default UITableViewCells, that so far seems to work well, but please reply with feedback if anyone has an idea of why this may not be a good solution! This is my first time working with trait collections or dynamic type.
Here is my solution, a static func
within a struct of Trait Collection Utility Functions (please read note below on when to not use this approach before using if interested in solving the same problem):
static func updateRowHeightForTraitCollection(forTableView tableView: UITableView) {
// This function sets cell height according to what the current current traitCollection's preferredContentSizeCategory is for dynamic text (adjusted by user in settings).
// If the content size category is .large or smaller, set the cell height to 53.0; otherwise, adjust cell height automatically according to content size.
if tableView.traitCollection.preferredContentSizeCategory > UIContentSizeCategory.large {
tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = 53.0
print("New traitCollection preferred content size: \(tableView.traitCollection.preferredContentSizeCategory); estimated row height = .automaticDimension")
} else {
tableView.rowHeight = 53.0
print("New traitCollection preferred content size: \(tableView.traitCollection.preferredContentSizeCategory); manually set row height = \(tableView.rowHeight)")
}
}
This would be called once when the view has loaded (within any table view configuration function/viewDidLoad
) and then called again in traitCollectionDidChange
in the following way:
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
if traitCollection.preferredContentSizeCategory != previousTraitCollection?.preferredContentSizeCategory {
TraitCollectionUtilities.updateRowHeightForTraitCollection(forTableView: tableView)
}
}
NOTE: One definite downside of this approach is that the cell will not automatically expand to fit text and that text will be truncated if the preferredContentSize is less than or equal to .large. Since the only tables I am using this approach with display only static cells, I just have to make sure that none of the text I want to display would take more than one line at the .medium content size. Any table view displaying non-static text should not use this approach.
Again, any other solutions or feedback would be welcome!
All my best,
-Thomas