Change UITableView style base on horizontalSizeClass

I have a UISplitViewController with a separate view controller for compact width. The master view controller shows a table view with a grouped style. Regular size class is insetGrouped and compact is grouped.

The view controllers have two paths, compact and regular width, but the only differences between the two are the table view style. The classes are the same as is the table view content.

I have to present a modal with a table view controller inside that needs to adapt to the size class. Before the modal is presented, I instantiate the right view controller for the current size class, however if the size class changes during presentation, the table view style doesn't change accordingly.

Is there a better way to handle the user changing the size class? Or just a general way to use a UISplitViewController and UITableView.Style with different size classes?

Many thanks for any solutions.
Answered by Frameworks Engineer in 656089022
UITableView does not support changing its style once created, so you would need to recreate the entire table view to switch to a new style.

However, the new UICollectionView list API introduced in iOS 14 does support dynamically changing the appearance of the list at any time (and you can even create a list with different appearances for different sections). This is the recommended approach for new code. Check out the WWDC video for an introduction: https://developer.apple.com/videos/play/wwdc2020/10026/

however if the size class changes during presentation

Why would this happen ? Because user rotates the device ?

Yes. The table view will load with the desired style when the view is presented but if the user rotates the device the style doesn't change.
Accepted Answer
UITableView does not support changing its style once created, so you would need to recreate the entire table view to switch to a new style.

However, the new UICollectionView list API introduced in iOS 14 does support dynamically changing the appearance of the list at any time (and you can even create a list with different appearances for different sections). This is the recommended approach for new code. Check out the WWDC video for an introduction: https://developer.apple.com/videos/play/wwdc2020/10026/
Thanks, that’s really useful.
I’m surprised that UICollectionView has a lot of UITableView features and more.
Change UITableView style base on horizontalSizeClass
 
 
Q