1
votes

I'm trying to embed UITableViews within a UIScrollView, with iOS6 and autolayout.

I've made it several times before iOS 6, with struts and springs. I added the tableviews as subviews of my scrollview, setting the appropriate frames and content sizes. It worked just fine, allowing the user to scroll between tableviews, but it doesn't work anymore for both a 3.5" screen AND the 4" screen of the iPhone 5.

The height of the scrollview is correctly resized, but the height of the subviews is not, and there is a blank space under them on 4" screens.

I made my ViewController in IB, added a UIScrollView, then I add the table views programmatically.

I've found a workaround which is setting the frame and size in the -viewDidAppear method, but it causes the scrollview to flash, as it's modified after the viewcontroller has been presented to the user.

I'm currently looking at NSLayoutConstraint, whithout much success for now.

Has anyone already been into the same problem?

Here is my current code:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self composeScrollView];
}

- (void)composeScrollView {
    // set up scrollview
    CGSize scrollViewContentSize = _scrollView.bounds.size;
    scrollViewContentSize.width *= self.tableViewsList.count;
    _scrollView.contentSize = scrollViewContentSize;

    CGPoint contentOffset = _scrollView.bounds.origin;
    contentOffset.x = _scrollView.bounds.size.width * self.tableIndex;
    _scrollView.contentOffset = contentOffset;

    _scrollView.directionalLockEnabled = YES;

    for (int i = 0; i < self.tableViewsList.count; i++) {        
        CGRect tableViewRect = _scrollView.bounds;
        tableViewRect.origin.x = tableViewRect.size.width *i;

        UITableView *tableView = [[UITableView alloc] initWithFrame:tableViewRect style:UITableViewStylePlain];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.directionalLockEnabled = YES;

        [_scrollView addSubview:tableView];
    }
}

I've seen this kind of layout in many iOS Apps, so I suppose the solution could be useful to many of us.

I'd love to add pictures to be more explicit, but I do not have enough reputation.

1

1 Answers

1
votes

Auto-layout hasn't set the frames yet in viewDidLoad. In viewDidAppear it has, so now it all works. The best method to do this sort of thing in is viewDidLayoutSubviews or viewWillLayoutSubviews.