Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reload the UIPageViewController view on orientation change

For an app I am building, I have an UIPageViewController inside a UIView with Transition Style = Scroll. Each of the pages have the same UITableViewController class (with different data of course). Upon this point all works correctly.

Now I want to have a landscape version or a portrait version of the UITableViewController depending on the orientation of the device. For this I implemented a NSNotification with a method being called on orientation change (orientationChanged), this also works. But from here a problem arises, when changing orientation of the device I am not sure how to load the correct UITableViewController instance. As you can see I am using the delegate method viewControllerAtIndex of the UIPageViewControllerDataSource in which I make sure the landscape or portrait version of the viewController is initiated.

Problem is that when changing orientation of the device the correct UITableViewController is loaded only after swiping left/right a couple of pages. I must be overlooking something simple, but cannot seem to find it!

I have the following code snippets, in which I think the problem is:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Create the data model
    _pageTitles = @[@"1",@"2",@"3",@"4",@"5"];
    _pageTables = @[@"test1",@"test2",@"test3",@"test4",@"test5"];

    // Create page view controller
    self.weightPageViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"WeightPageViewController"];
    self.weightPageViewController.dataSource = self;

    WeightPageContentViewController *startingViewController = [self viewControllerAtIndex:0];
    NSArray *viewControllers = @[startingViewController];
    [self.weightPageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

    [self addChildViewController:_weightPageViewController];
    [self.view addSubview:_weightPageViewController.view];
    [self.weightPageViewController didMoveToParentViewController:self];

    self.edgesForExtendedLayout = UIRectEdgeNone;

    //Register for device orientation changes.
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
}

- (void) orientationChanged:(id)object {
    [self viewControllerAtIndex:0];
}

- (WeightPageContentViewController *)viewControllerAtIndex:(NSUInteger)index
{
    if (([self.pageTitles count] == 0) || (index >= [self.pageTitles count])) {
        return nil;
    }

    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    if (UIDeviceOrientationIsLandscape(deviceOrientation)) {
        NSLog(@"Orientation is now Landscape);
        // Create a new view controller and pass suitable data.
        WeightPageContentViewController *weightPageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"WeightPageContentViewControllerLandscape"];
        weightPageContentViewController.tableText = self.pageTables[index];
        weightPageContentViewController.titleText = self.pageTitles[index];
        weightPageContentViewController.pageIndex = index;
        return weightPageContentViewController;
    } else if (deviceOrientation == UIDeviceOrientationPortrait) {
        NSLog(@"Orientation is now Portrait);
        // Create a new view controller and pass suitable data.
        WeightPageContentViewController *weightPageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"WeightPageContentViewController"];
        weightPageContentViewController.tableText = self.pageTables[index];
        weightPageContentViewController.titleText = self.pageTitles[index];
        weightPageContentViewController.pageIndex = index;
        return weightPageContentViewController;
    } else {
        NSLog(@"Orientation is now unknown);
        // Create a new view controller and pass suitable data.
        WeightPageContentViewController *weightPageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"WeightPageContentViewController"];
        weightPageContentViewController.tableText = self.pageTables[index];
        weightPageContentViewController.titleText = self.pageTitles[index];
        weightPageContentViewController.pageIndex = index;
        return weightPageContentViewController;
    }
}
like image 560
Hugo Avatar asked Dec 31 '13 10:12

Hugo


2 Answers

Every time the orientation changes you need to again set the view controller

WeightPageContentViewController *startingViewController = [self viewControllerAtIndex:currentViewControllerIndex];
NSArray *viewControllers = @[startingViewController];
[self.weightPageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
like image 134
Jay Gajjar Avatar answered Nov 11 '22 06:11

Jay Gajjar


For me this simple solution worked:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [self.pageController.view setNeedsLayout];
}
like image 28
electronix384128 Avatar answered Nov 11 '22 06:11

electronix384128