Search code examples
iosobjective-cxcodeuitableviewamslidemenu

Enable scrolling in UITableViewController


I want to get my UITableViewController to scroll only when there is not enough room on the screen for all of its cells. I am using static cells that are designed to be shown in an iPhone 5, 6, and 6 plus size screen. However, when shown in the 4s screen, the bottom cells get cut off.

Ideally I would like to use AutoLayout to anchor the bottom of the tableview to the bottom of its superview as I have with other tableviews in my application, but Xcode doesn't allow me to add constraints to the UITableView in a UITableViewController. The reason I have to use the UITableViewController is because I am using a pod for a slide menu (https://github.com/SocialObjects-Software/AMSlideMenu) that subclasses UITableViewController.

I have tried to change the size of the UITableView's frame based on the screen size because I assumed a scroller would automatically be added if the cells took up more room than the containing frame. I used the following code to do so:

CGRect frame = self.tableView.frame;
[self.tableView setFrame:CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, [[UIScreen mainScreen] bounds].size.height - HEADER_HEIGHT)];

However, it had no effect on the way the table was displayed.

Does anyone know how I might be able to set the bottom of the UITableView dynamically and add a scroller to the cells when the screen is too small? I would also welcome any suggestions that might help avoid having to do this at all, as I would prefer not having to do anything too hacky.


Solution

  • You can set the alwaysBounceVertical property to false so the tableView will only scroll when its contentSize is larger than the table view's frame which you can constrain to your view however you like.

    tableView.alwaysBounceVertical = NO;