avatarPavlos Simas

Summary

The web content provides a solution for embedding a UITableView within a UIScrollView in Swift, allowing the table view to dynamically adjust its height to fit all its content and remain scrollable within the scroll view.

Abstract

The article details a method for integrating a UITableView into a UIScrollView such that the table view's height automatically adjusts to accommodate its entire content set. This is achieved by subclassing UITableView and overriding properties like intrinsicContentSize and contentSize, as well as the reloadData method. The custom class, TableViewAdjustedHeight, ensures that the table view's intrinsic content size is invalidated whenever the content size changes, prompting the layout system to recalculate the size during the next layout pass. The article also guides readers through setting up a storyboard or xib with the appropriate constraints and custom class settings for the table view. Additionally, it provides a visual layout example and instructions for disabling scrolling options on the table view since scrolling will be managed by the parent UIScrollView. The article concludes with a call to support the author by joining Medium through a referral link and promotes a SwiftUI learning resource.

Opinions

  • The author suggests that using a UITableView with dynamic height within a UIScrollView is a common challenge that their solution addresses effectively.
  • Overriding reloadData is necessary to ensure the table view's intrinsic content size is recalculated when data changes.
  • The article implies that using constraints and the new layout guides in conjunction with the custom table view class is the recommended approach for layout management.
  • The author emphasizes the importance of disabling the table view's scrolling abilities when it is embedded within a scroll view to avoid conflicts in scroll behavior.
  • The article encourages readers to support the author's work and offers additional educational resources for those interested in learning more about SwiftUI.

Automatic Height for UITableView into UIScrollView Swift

Insert an UITableView that takes all the height of it’s content and will be scrollable through UIScrollView

Photo by Tranmautritam from Pexels

Let’s see how to insert an UITableView into a UIScrollView and make the scrollView content height based on all of the UITableView items.

First we need to create our UITableView class that we will use into our storyboards or classes. It will be very simple:

import UIKit
class TableViewAdjustedHeight: UITableView {
    override var intrinsicContentSize: CGSize {
        self.layoutIfNeeded()
        return self.contentSize
    }
override var contentSize: CGSize {
        didSet {
            self.invalidateIntrinsicContentSize()
        }
    }
override func reloadData() {
        super.reloadData()
        self.invalidateIntrinsicContentSize()
    }
}

The trick here is the didSet of the contentSize that calls the invalidateIntrinsicContentSize which should be the same as the contentSize.

We also override the reloadData function because we need to call again the invalidateIntrinsicContentSize function when we refresh the data for our UITableView.

What invalidateIntrinsicContentSize() do?

Call this when something changes in your custom view that invalidates its intrinsic content size. This allows the constraint-based layout system to take the new intrinsic content size into account in its next layout pass.

The next thing that you should do is to configure your storyboard/xib with constraints and change the UITableView settings (those can be changed also from the file that we created)

Now let’s create a new UIViewController into your storyboard and add a UIScrollView. Inside your scrollView put your views and the TableViewAdjustedHeight that we have already created. You can check this article if you need information how to configure a UIScrollView using Content and Frame Layout Guide:

Our Layout may look like this (in our example with have a date, some buttons, a trasparent view in the middle and a UITableView in the bottom.

Tap on the UITableView and in the right panel you can set the Custom Class that you want to use. In our case just put TableViewAdjustedHeight. The module is usually automatically pre filled with your project’s name.

And here are the UITableView settings

You need to disable all the scrolling options because now it will take the height that needs for all it’s cells and it will be scrollable through the UIScrollView and not the UITableView anymore.

Run project and you are ready! Your UITableView will have the height based on all the cells that it has and it will be scrollable through your scrollview that may contain other items too!

Support us by joining Medium through this link:

Also if you need learn about SwiftUI visit the following link:

Uitableview
iOS
Swift
iOS Development
Xcode
Recommended from ReadMedium