I want a UITableView inside of a UIScrollView to extend to the bottom of the UIScrollView and not get cut off by initial screen

19,527

Solution 1

I suggest you to use tableView only. You can create your top view in the header of tableView. Doing this there will be no need to maintain the content size of scrollView.

Solution 2

When two scrollable views with the same direction are put to each other, it always cause problems. I think in your case would be better to place only UITableView on the screen: you can whether place UITableView within another non-scrollable view, that holds you static content, our put your static content as a header for UITableView. Let me know if you need me to elaborate the idea. Hope, it'll help.

Solution 3

i think you should do this:

first: (see https://forums.developer.apple.com/thread/81895)

tableView.estimatedRowHeight = 0 tableView.estimatedSectionHeaderHeight = 0 tableView.estimatedSectionFooterHeight = 0 tableView.rowHeight = 70

then

tableView.frame.size.height = tableView.contentSize.height

Share:
19,527
Ethan Zhao
Author by

Ethan Zhao

Updated on June 06, 2022

Comments

  • Ethan Zhao
    Ethan Zhao almost 2 years

    so I have a viewcontroller that I put a scrollview in. Inside the scrollview, I put a bunch of items in the top half, like an image, labels, and buttons. On the bottom half, I put a uitableview. I set all the constraints and everything. I used the following code:

    let bounds: CGRect = UIScreen.main.bounds
    let w:Int  = Int(bounds.size.width)
    let h:Int = Int(chaptersTableView.contentSize.height)
    let temp: Int = 300 + h
    scrollView.contentSize = CGSize(width: w, height: temp)
    chaptersTableView.frame = CGRect (x: 0, y: 280.5, width: self.view.frame.size.width, height: CGFloat(44 * sampleModel.objectChapterList.count))
    

    I wanted to set the contentsize of the scrollview, and then set the height of my tableview frame.

    The tableview is meant to contain many items, so I want the user to be able to scroll down, and the entire contents of the scrollview to scroll down to reveal more of the tableview. However, the tableview always gets cut off and when I scroll down in the scrollview, the table ends. I have to scroll inside of the tableview to access more cells.

    Even when I replace the "44 * (number of cells in my table)" with a random number like 100, it will at first cut the table off so that is it short, but then when I tap the screen, the table will change to resume being full length to the bottom of the screen (but not to bottom of scrollview).

    I thought about making all the stuff in the top half of the scrollview just another cell of the tableview so that it can be part of the table and I can avoid this hassle, but that would be a lot of work so I wanted to see if I could do this.

    If you need pictures or something, I can provide them. If I'm not being clear, make sure to comment and I can explain better.