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

前端之家收集整理的这篇文章主要介绍了ios – UIPageViewController不调整其子视图控制器的大小前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个视图控制器以编程方式设置,它具有一个子UIPageViewController.这显示了也以编程方式设置的各种其他视图控制器.这些视图控制器的视图将AutotoresizingMaskIntoConstraints设置为YES,但是页面视图控制器的视图使用约束将自身定位在顶视图控制器中.

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

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

  1. self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
  2. self.pageViewController.view.backgroundColor = [UIColor clearColor];
  3. self.pageViewController.view.translatesAutoresizingMaskIntoConstraints = NO;
  4. self.pageViewController.dataSource = self;
  5. self.pageViewController.delegate = self;
  6. [self.pageViewController willMoveToParentViewController:self];
  7. [self.view addSubview:self.pageViewController.view];
  8. [self addChildViewController:self.pageViewController];
  9. [self.pageViewController didMoveToParentViewController:self];
  10.  
  11. self.currentPageIndex = 0;
  12.  
  13. self.currentPageController = [self pageForIndex:self.currentPageIndex];
  14. self.currentPageController.delegate = self;
  15. [self.pageViewController setViewControllers:@[self.currentPageController] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:NULL];

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

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

谢谢!

解决方法

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

这是我的工作代码(iOS 8.0及更高版本). childController1在故事板上设置了所有使用约束的布局,并用[self.storyboard instantiateViewControllerWithIdentifier:@“ChildControllerId”]实例化.

  1. - (void)createPageController
  2. {
  3. ...
  4. pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
  5. pageController.dataSource = self;
  6. [pageController setViewControllers:@[childController1] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
  7. [self addChildViewController:pageController];
  8. [self.view addSubview:pageController.view];
  9. UIView *pageView = pageController.view;
  10. pageView.translatesAutoresizingMaskIntoConstraints = NO;
  11. [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[pageView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(pageView)]];
  12. [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[pageView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(pageView)]];
  13. [pageController didMoveToParentViewController:self];
  14. ...
  15. }
  16. - (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
  17. {
  18. [pageController.view setNeedsLayout];
  19. [super willTransitionToTraitCollection:newCollection withTransitionCoordinator:coordinator];
  20. }

希望它有帮助.

猜你在找的iOS相关文章