iosobjective-cuitableviewuiscrollviewcontentsize

Tableview frame issue when put in a scrollview


I wanted to post a gif but apparently I don't have enough reputation. Oh well, whatever; I was using UIPageViewController, but for some reason I decided to go with a more manual solution by using UIScrollView and adding the views of UITableViewControlllers to the corresponding offsets (pages). I have 4 UItableViewControllers on each page (the views of table view controllers) and all of these are added to the container view controller (which has the UIScrollView) as child view controllers.

The actual problem is when I made the switch, table views began refusing to go all the way down and part of the final table view cell stays trimmed by the end of the screen when the scrolling ends.

enter image description here

So, wanted to ask if anyone came across something like this before of know how to get rid of this. I know I could always use a library, but I want to learn. Here is some code:

_containerScrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
_containerScrollView.contentSize = CGSizeMake(_containerScrollView.frame.size.width * 4, 0.0f);
_containerScrollView.pagingEnabled = YES;

[self.view addSubview:_containerScrollView];

UITableViewController *vc1 = [[UIViewController alloc] init];
UITableViewController *vc2 = [self.storyboard instantiateViewControllerWithIdentifier:@"trendingViewController"];
UITableViewController *vc3 = [self.storyboard instantiateViewControllerWithIdentifier:@"placesViewController"];
UITableViewController *vc4 = [self.storyboard instantiateViewControllerWithIdentifier:@"favoritesViewController"];

self.rallyViewControllers = [NSArray vc1, vc2, vc3, vc4, nil];

[self addViewControllers];

Other methods;

- (void)addViewControllers{

     if (self.rallyViewControllers == nil) {

        return;

     }

     for (int i = 0; i < [self.rallyViewControllers count]; i++) {

         UIViewController* viewController = [self.rallyViewControllers objectAtIndex:i];

         [self addChildViewController:viewController];
         [viewController didMoveToParentViewController:self];

         [_containerScrollView addSubview:viewController.view];

     }

}

This is called in viewDidLayoutSubviews

- (void)configureFrames{

    if (self.rallyViewControllers == nil) {

    return;

    }

    CGFloat width = _containerScrollView.frame.size.width;
    CGFloat height = _containerScrollView.frame.size.height;

    for (int i = 0; i < [self.rallyViewControllers count]; i++) {

        UIViewController *viewController = [self.rallyViewControllers objectAtIndex:i];
        viewController.view.frame = CGRectMake(i * width, 0.0f, width, height);

    }

}

Solution

  • I got it. The problem was when I added the view controllers views to the scroll view, they returned dimensions as if there was no navigation bar. But they were still positioned under the navigation bar, which caused total view to have 64 pt more height than the screen could show. when I manually subtracted 64 pt from views, and it was fixed. But since that is a very crude way of doing this, I then tried to fix it by fiddling with auto-layout, which ended up fine.