ios – UIPageViewController不调整其子视图控制器的大小

我有一个视图控制器以编程方式设置,它具有一个子UIPageViewController.这显示了也以编程方式设置的各种其他视图控制器.这些视图控制器的视图将AutotoresizingMaskIntoConstraints设置为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,但它是一个笨拙的旋转动画,而我滑动的下一页也没有正确调整大小.

为什么页面浏览控制器不能调整其子视图控制器的大小,我该怎么做呢?

谢谢!

解决方法

首先,以自动布局,编程方式或故事板设置所有内容.然后,抓住父视图控制器中的方向更改,并强制UIPageViewController的视图重新布局.

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

希望它有帮助.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...