UIPageViewController在轮换

时间:2015-06-30 14:26:35

标签: ios ipad uipageviewcontroller screen-orientation nsautolayout

我正在尝试使用添加了视图控制器的UIPageViewController,问题是如果我以纵向模式启动应用程序,它看起来非常像这样:

enter image description here

但如果我将设备旋转到横向模式,它会显示如下:

enter image description here

虽然分页控件已正确调整大小,但添加的视图控制器的视图未正确调整大小。

下面是我用来在AppDelegate中将各个视图控制器添加为根视图控制器的代码:

pagesContainerViewController = [[RWPagesContainerViewController alloc] initWithNibName:@"RWPagesContainerViewController" bundle:nil];
[pagesContainerViewController loadPaginationControlAtIndex:0];
self.window.rootViewController = pagesContainerViewController;

这是loadPaginationControlAtIndex方法的实现:

- (void)loadPaginationControlAtIndex:(RWPaginationView)viewIndex {
    _pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];

    FirstViewController *firstViewController = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
    SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    ThirdViewController *thirdViewController = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil];

    _subviewControllers = @[firstViewController, secondViewController, thirdViewController];

    [self.pageController setViewControllers:@[_subviewControllers[viewIndex]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];

    [self addChildViewController:self.pageController];
    [[self view] addSubview:[self.pageController view]];

    UIView *insertedView = self.pageController.view;
    insertedView.translatesAutoresizingMaskIntoConstraints = NO;
    self.view.translatesAutoresizingMaskIntoConstraints = NO;

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[insertedView]|"
                                                                      options:0
                                                                      metrics:nil
                                                                        views:NSDictionaryOfVariableBindings(insertedView)]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[insertedView]|"
                                                                      options:0
                                                                      metrics:nil
                                                                        views:NSDictionaryOfVariableBindings(insertedView)]];

    [self.view layoutIfNeeded];

    self.pageController.dataSource = self;
}

我错过了什么吗?请建议。

1 个答案:

答案 0 :(得分:1)

加载应用程序或加载视图时

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter]
addObserver:self selector:@selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:[UIDevice currentDevice]];

然后添加以下方法:

- (void) orientationChanged:(NSNotification *)note
{
   [self.pageController setViewControllers:@[_subviewControllers[currentIndex]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];   
}