Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIPageViewController not resizing its child view controllers

I have a view controller set up programmatically which has a child UIPageViewController. This displays various other view controllers that are also set up programmatically. These view controllers' views have translatesAutoresizingMaskIntoConstraints set to YES, but the page view controller's view uses constraints to position itself in the top view controller.

The problem is, when the user rotates the device the page view controller resizes its frame but its child view controllers don't. By didRotateFromInterfaceOrientation, its frame is still from the old orientation. I've verified that the rotation methods are called on the child view controllers, their frame just doesn't change.

I set up the page view controller like this:

self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
self.pageViewController.view.backgroundColor = [UIColor clearColor];
self.pageViewController.view.translatesAutoresizingMaskIntoConstraints = NO;
self.pageViewController.dataSource = self;
self.pageViewController.delegate = self;
[self.pageViewController willMoveToParentViewController:self];
[self.view addSubview:self.pageViewController.view];
[self addChildViewController:self.pageViewController];
[self.pageViewController didMoveToParentViewController:self];

self.currentPageIndex = 0;

self.currentPageController = [self pageForIndex:self.currentPageIndex];
self.currentPageController.delegate = self;
[self.pageViewController setViewControllers:@[self.currentPageController] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:NULL];

I've tried calling setNeedsLayout but it's a clumsy rotation animation and the next page that I swipe to is also not resized properly.

Why is the page view controller not resizing its child view controllers, and how do I get it to do this?

Thanks!

like image 682
architectpianist Avatar asked Jul 21 '13 05:07

architectpianist


1 Answers

First, set up everything with auto layout, programmatically or Storyboard. Then, catch the orientation change in the parent view controller and force the UIPageViewController's view to layout again.

This is my working code (iOS 8.0 and later). childController1 was set up on a Storyboard with all layout using constraints, and instantiated with [self.storyboard instantiateViewControllerWithIdentifier:@"ChildControllerId"].

- (void)createPageController
{
...
    pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
    pageController.dataSource = self;
    [pageController setViewControllers:@[childController1] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
    [self addChildViewController:pageController];
    [self.view addSubview:pageController.view];
    UIView *pageView = pageController.view;
    pageView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[pageView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(pageView)]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[pageView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(pageView)]];
    [pageController didMoveToParentViewController:self];
...
}
- (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
    [pageController.view setNeedsLayout];
    [super willTransitionToTraitCollection:newCollection withTransitionCoordinator:coordinator];
}

Hope it helps.

like image 125
camilomq Avatar answered Nov 02 '22 08:11

camilomq