UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Why does estimatedHeightForRowAt not return the provided height in UITableView?

Forums > Swift

Hi everyone,

In my app, I have used this code in an attempt to make cell height adjust to fit larger text sizes:

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  return UITableView.automaticDimension
}

override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
  return 53.0
}

This works perfectly for automatic adjustment when the cell height needs to increase with dynamic type.

However, when the system is set to the default (medium) dynamic type size, the cell height decreases below the 53.0 height I specified, to what I assume is the standard default cell height (around 40.0).

Is it possible to set a minimum cell size of 53.0, so that automaticDimension never returns a height lower than one I've provided? Or is this impossible to do with automatically adjusting cell height?

Thanks so much,

-Thomas

3      

Hi, do you have constraits for the label pinned to the cells contentView?

As an option I would maybe try creating greaterThan height constraint with the value of 53?

4      

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

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.