【问题标题】:UIPageViewController not resizing its child view controllersUIPageViewController 没有调整其子视图控制器的大小
【发布时间】:2013-07-20 02:32:02
【问题描述】:

我有一个以编程方式设置的视图控制器,它有一个子 UIPageViewController。这将显示各种其他也以编程方式设置的视图控制器。这些视图控制器的视图将translatesAutoresizingMaskIntoConstraints 设置为YES,但页面视图控制器的视图使用约束将自身定位在顶部视图控制器中。

问题是,当用户旋转设备时,页面视图控制器会调整其框架的大小,但其子视图控制器不会。通过didRotateFromInterfaceOrientation,它的框架仍然来自旧的方向。我已经验证在子视图控制器上调用了旋转方法,它们的框架没有改变。

我这样设置页面视图控制器:

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

我尝试调用setNeedsLayout,但这是一个笨拙的旋转动画,而且我滑动到的下一页也没有正确调整大小。

为什么页面视图控制器不调整其子视图控制器的大小,我该如何让它这样做?

谢谢!

【问题讨论】:

  • 您找到解决问题的方法了吗?我被同样的问题困住了。
  • 问题是子控制器上的 -didRotateFromInterfaceOrientation 永远不会被调用。我通过从父视图控制器 -didRotateFromInterfaceOrientation 手动调用它来解决它。
  • 您调用 clild 视图控制器上的什么方法来处理方向变化?
  • 我有同样的问题,但不是旋转,它是多任务视图。如果您找到任何解决方案,请分享它

标签: ios constraints autolayout uipageviewcontroller autorotate


【解决方案1】:

将控制器的视图直接添加到另一个控制器的视图中并期望调用轮换和生命周期方法并不是最好的策略。我觉得将它添加到控制器层次结构中可能会起作用,但我不能对此发表太多评论。

我使用标签栏导航器进行了测试,我的 UIPageViewController 得到了事件以及 UIPageViewController 中显示的控制器。我猜如果我将控制器推入 UINavigationController 我会得到相同的结果。

我建议您以比将其视图添加到另一个控制器的视图更标准的方式显示您的 UIPageVIewController。

【讨论】:

  • 页面视图控制器不必总是全屏。在 iPad 等设备上,它可以是视图的一部分。因此,它必须按照问题中提到的方式实施。如果有其他方法可以实现,请纠正我。
【解决方案2】:

首先,使用自动布局、以编程方式或 Storyboard 设置所有内容。然后,在父视图控制器中捕捉方向变化并强制 UIPageViewController 的视图重新布局。

这是我的工作代码(iOS 8.0 及更高版本)。 childController1 设置在 Storyboard 上,所有布局都使用约束,并使用 [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];
}

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-29
    • 1970-01-01
    • 1970-01-01
    • 2018-01-19
    相关资源
    最近更新 更多