UIPageViewController没有调整其子视图控制器的大小。

13

我以编程方式设置了一个视图控制器,并在其中设置了一个子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,但它是一个笨拙的旋转动画,而且我滑动到的下一页也无法正确地调整大小。

为什么页面视图控制器没有调整其子视图控制器的大小,我该如何做到这一点?

谢谢!


你找到解决问题的方法了吗?我也卡在同样的问题上了。 - AlexR
问题在于子控制器上的-didRotateFromInterfaceOrientation从未被调用。我通过从父视图控制器的-didRotateFromInterfaceOrientation手动调用它来解决了这个问题。 - Mani
你调用了哪个子视图控制器的方法来处理方向变化? - Satyam
我有同样的问题,但不是针对旋转,而是针对多任务视图。如果您找到任何解决方案,请分享。 - Johnykutty
2个回答

6

首先,使用自动布局、编程或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];
}

希望这有所帮助。

-2
将控制器的视图直接添加到另一个控制器的视图中,并期望调用旋转和生命周期方法并不是最佳策略。我觉得将其添加到控制器层次结构中可能会起作用,但我对此不能发表太多评论。
我使用选项卡栏导航器进行了测试,我的UIPageViewController与在UIPageViewController中显示的控制器一起获得了事件。我猜如果我将控制器推入UINavigationController中,我会得到相同的结果。
我建议您以比将其视图添加到另一个控制器的视图更标准的方式显示您的UIPageVIewController。

页面视图控制器并不一定总是必须全屏显示。在像iPad这样的设备上,它可以成为视图的一部分。因此,必须按照问题中所述的方式进行实现。如果有其他方法可以实现,请纠正我。 - Satyam

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接