I'm embedding a UITableView
inside of another UIScrollView
. I only want the UIScrollView
to scroll, not the UITableView
, so I want to disable the scrolling in UITableView
, as well as expand the contentHeight
for the UITableView
to accommodate all dynamic at once.
How would I go about doing this?
Basically, tableview's content size is all you need.
Set tableview scrolling to
false
because we don't want scolling behaviour.Get the reference of tableview's height constraint.
set
tableViewHeightConstraint.constant = tableView.contentSize.height
Make sure, tableview's top, bottom, leading and trailing constraint and are in-place.
While this is very simple to accomplish with a fixed-content tableview as a subview in a scrollview, what is it you are exactly trying to accomplish? Dynamic cells with fixed content above and below? Permanently displayed content with a sliding section in the center?
Perhaps a cleaner design would be using a single UITableView with a Table Header and Footer, or a Section Header an Footer leveraging the automatic behavior provided by UITableViewStyleGrouped vs UITableViewStylePlain
That said, other answers seem to form a decent solution:
Another option for manipulating scroll behavior would be checking the tableview or scrollview in the delegate scrollView shouldStartScrolling or similar methods, as this would allow more flexibility over the UI controls
You need to set the
UITableView
scroll tofalse
,And then you can add the
UITableView
onUIScrollView
by setting it's content size asUITableView
height.Set tableView size equal with contentSize.
UITableView
Scrolling toNO
Second, create a
height constraint
for the table view and after you call[UITableView reloadData]
change yourheight constraint