Hi fellow programmers.
I've spent many hours trying to design this layout in Swift, with little success.
The idea is having two TableViews:
- The top one, with zero or more (indefined) cells, which can't be scrollable and its height grows with the number of cells.
- The bottom one, with zero or more (indefined) cells, which is scrollable, and thus its height adapts to the space left from the latter tableview.
I've already tried to play with the constraints programmatically, by setting both tables top constraint to the top of the screen and then modifying that constraint depending on the size of the top TableView. Doesn't seem to work.
I also tried to modify the height of the tables programmatically, based on the number of cells that will be draw. Didn't work either.
Honestly, I'm at a deadpoint right now, and I'm looking out for help or some new ideas to make it work. Any help will be highly appreciated. Thanks in advance!
You'll need to decide on some additional logic - as in, what to do when there are too many rows in the top table view, but this is one approach:
Use this custom table view class (from https://stackoverflow.com/a/48623673/6257435):
This will "auto-size" your table view whenever the content size changes. To use it, add a table view and set its class to
ContentSizedTableView
.200
let's you work with the layout. But... edit that height constraint and check the Placeholder - Remove at build time checkbox.Now, when you add rows to the
top table view
, it will auto-expand vertically, and thebottom table view
will auto-contract vertically (until it disappears). Because the bottom of thetop table view
is still constrained to the top of thebottom table view
, thetop table view
will stop growing and be scrollable (instead of extending off the bottom of the view).Here's a very simple example:
With 8 rows in the top, and 12 rows in the bottom, we get:
and with 11 rows in the top:
and finally with 17 rows in the top (scrolled so you can see):
Here is the storyboard source for that example: