arc*_*ist 13 constraints ios auto-rotation uipageviewcontroller autolayout
我有一个以编程方式设置的视图控制器,它有一个孩子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];
Run Code Online (Sandbox Code Playgroud)
我试过调用,setNeedsLayout但这是一个笨拙的旋转动画,我刷到的下一页也没有正确调整大小.
为什么页面视图控制器没有调整其子视图控制器的大小,我该如何让它执行此操作?
谢谢!
小智 6
首先,使用自动布局,编程或故事板设置所有内容.然后,捕获父视图控制器中的方向更改并再次强制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];
}
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你.
小智 -2
将一个控制器的视图直接添加到另一个控制器的视图中并期望调用旋转和生命周期方法并不是最好的策略。我觉得将它添加到控制器层次结构中可以工作,但我无法对此发表太多评论。
我使用标签栏导航器进行了测试,我的 UIPageViewController 获取了事件以及 UIPageViewController 中显示的控制器。我猜如果我将控制器推入 UINavigationController 中,我会得到相同的结果。
我建议您以更标准的方式显示 UIPageVIewController,而不是将其视图添加到另一个控制器的视图。
| 归档时间: |
|
| 查看次数: |
3255 次 |
| 最近记录: |