Self Sizing Cells and Dynamic Type
Xcode comes with a feature for
UITableView known as Self Sizing Cells. To me, this was seriously one of the most exciting features for the SDK at the time. Prior to iOS 8, if you wanted to display dynamic content in a table view with variable heights, you would need to calculate the row height manually. In iOS 11, Apple's engineers take this feature even further. The self-sizing feature is enabled automatically. In other words, header views, footer views and cells use self-sizing by default for displaying dynamic content.
While this feature is now enabled without the need of configurations in iOS 15, I want you to understand what happens under the hood. In brief, here is what you need to do when using self sizing cells:
- Define auto layout constraints for your prototype cell
- Specify the
estimatedRowHeightproperty of your table view
- Set the
rowHeightproperty of your table view to
If we express the last point in code, it looks like this:
tableView.estimatedRowHeight = 95.0
tableView.rowHeight = UITableView.automaticDimension
This is what iOS 14 (or up) has done for you.
With just two lines of code, you instruct the table view to calculate the cell's size to match its content and render it dynamically. This self sizing cell feature should save you tons of code and time. You're going to love it.
In the next section, we'll develop a simple demo app to demonstrate self sizing cell. There is no better way to learn a new feature than to use it. In addition to self sizing cell, I will also talk about Dynamic Type. Dynamic Type was first introduced in iOS 7 - it allows users to customize the text size to fit their own needs. However, only apps that adopt Dynamic Type respond to the text change.
You're encouraged to adopt Dynamic Type so as to give your users the flexibility to change text sizes, and to improve the user experience for vision-challenged users. Therefore, in the later section you will learn how to adopt dynamic type in your apps.
To continue reading and access the full version of the book, please get the full copy here. You will also be able to access the full source code of the project.