UIPageViewController 没有在旋转时调整其子视图控制器的大小

Posted

技术标签:

【中文标题】UIPageViewController 没有在旋转时调整其子视图控制器的大小【英文标题】:UIPageViewController not resizing its child view controllers on rotation 【发布时间】:2015-06-30 14:26:35 【问题描述】:

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

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

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

下面是我用来在 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】:

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

[[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];   

【讨论】:

以上是关于UIPageViewController 没有在旋转时调整其子视图控制器的大小的主要内容,如果未能解决你的问题,请参考以下文章

UIPageViewController 有三个 UIViewControllers 没有 UIScrollView

为啥我的 UIPageViewController 中的页面没有改变?

UIPageViewController 带有最后一个视图,没有页面指示器

UIPageViewController - 如果没有页面,则不允许移动

当兄弟是 UIPageViewController 时,UITableView 没有动画

UIPageViewController 手势正在调用 viewControllerAfter: 但没有动画