Search code examples
ios6scrollviewtableviewautolayout

iOS 6 autolayout scrollview not scrolling with two tables


I have a scrollview with two tableviews side by side, and a bottom navigation bar.

Both tableviews should consume the entirety of the view, but scroll together if their content is too large for the screen. The two tableviews show different data of the same dataset.

view hierarchy

When the table content is larger than the screen, or I rotate the phone to landscape, then there's no scrolling. There doesn't appear to be any scrolling at all.

I'm trying to stick with autolayout. What am I doing wrong here? The tableviews are sized to be the entire size of the view, and navigation bar is set to be 0 away from the bottom of the view. It sticks correctly, but scrolling doesn't work at all.

What am I supposed to do to get scrolling working?


Solution

  • UITableView is itself a UIScrollView - which means that if its interior content is larger than its size it will scroll. I've had to deal with table views inside scroll views before and I think that turning off scrolling does not force the table view to be the height of its interior content, but instead it clips it to the visible area. To get a table view to fit the interior content, you need to add a height constraint to the table view and drag it into your controller to create an outlet. Then in your controller add something like this (updated to match your constraint outlet names)

    -(void) viewWillLayoutSubviews{
        [self.tableViewOne layoutIfNeeded];
        self.tableViewOneHeightConstraint.constant = self.tableViewOne.contentSize.height;
    
        [self.tableViewTwo layoutIfNeeded];
        self.tableViewTwoHeightConstraint.constant = self.tableViewTwo.contentSize.height;
    }
    

    Make sure that you have constraints from the bottom of your table views to the scroll view too, so that your main scroll view knows to fit the interior content.