gpt4 book ai didi

containers - UIPageViewController 在容器 View Controller 中的旋转

转载 作者:行者123 更新时间:2023-12-04 16:00:31 25 4
gpt4 key购买 nike

我有一个包含其他 ViewControllers 的 UIViewController。初始 ViewController 在 vi​​ewDidLoad 中设置:

FirstViewController *first = [FirstViewController alloc] init];
first.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
first.view.frame = m_content.frame;

[self addChildViewController:first];
[m_content.view addSubview:first.view];
[first didMoveToParentViewController:self];
m_activeViewController = first;

此容器 Controller 已实现 automaticForwardAppearanceAndRotationMethodsToChildViewControllers 返回是。它还实现了对非事件 View Controller 的手动正向旋转更改
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
for(UIViewController *vc in m_viewControllers)
{
if(vc != [m_activeViewController]
[vc willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
}
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
for(UIViewController *vc in m_viewControllers)
{
if(vc != [m_activeViewController]
[vc didRotateFromInterfaceOrientation:fromInterfaceOrientation];
}
}

当点击菜单按钮时,我会在 ViewControllers 之间进行转换。
- (void)onMenuItemTapped:(id)sender
{
UIViewController *vc = [m_viewControllers objectAtIndex:sender.tag];

vc.view.frame = m_content.frame;
[self addChildViewController:vc];
[self transitionFromViewController:m_activeViewController toViewController:vc duration:0 options:UIViewAnimationOptionTransitionNone animations:nil completion:^(BOOL finished) {
[vc didMoveToParentViewController:self];
[m_activeViewController removeFromParentViewController];
m_activeViewController = vc;
}];
}

这种过渡适用于我的“普通” View Controller ,并且它们在方向更改后正确显示,即使它们未处于事件状态。但是,这些 subview Controller 之一,称为 SecondCV,具有 UIPageViewController 作为 subview Controller 。我有 UIPageViewControllerDelegate UIPageViewControllerDataSource 设置为此 SecondCV 并在 中pageViewController:spineLocationForInterfaceOrientation: 我为纵向返回 UIPageViewControllerSpineLocationMin,为横向返回 UIPageViewControllerSpineLocationMid。此 SecondVC 的旋转在其处于事件状态时可以正常工作 - 横向有两个页面,一个纵向模式正确显示。但是当这个 SecondVC 不活动时,旋转是不正确的。即使 pageViewController:spineLocationForInterfaceOrientation: 被调用,在纵向和横向模式下仍然有一个页面。我正在尝试解决此问题一段时间,但我没有看到任何其他选项。你有任何想法如何解决这个问题吗?

谢谢你。

最佳答案

我已经解决了这种棘手的问题。

实际上,SecondVC 已正确旋转,或者至少,脊椎位置已正确更改。因此,当我旋转设备并且 SecondVC 在父 View 中不是事件 View Controller 时,它 收到轮换消息和 pageViewController:spineLocationForInterfaceOrientation: 也被称为。我为新界面方向设置了正确的脊椎位置,并调用了 setViewControllers 到 SecondVC 的页面 View Controller 。问题是,即使我为 设置了两个 View Controller UIPageViewControllerSpineLocationMid ,在显示 SecondVC 之后仍然只有一个,就像在 UIPageViewControllerSpineLocationMin 中一样。

通过在 中将 View Controller 设置为页面 View Controller ,我已经“修复”了这个问题。 View 将出现:再次,因为当调用此方法时,页面 View Controller 的脊椎位置是正确的,并且基于此信息我设置了一个或两个页面( View Controller )

- (void)viewWillAppear:(BOOL)animated
{
if(m_pageController.spineLocation == UIPageViewControllerSpineLocationMid)
{
LeftPageController *left;
RightPageController *right;
if(m_pageController.viewControllers.count > 0)
{
left = [m_pageController.viewControllers objectAtIndex:0];

right = [m_storyboard instantiateViewControllerWithIdentifier:@"rightPage"];
[right loadView];
}

[m_pageController setViewControllers:[NSArray arrayWithObjects:left, right, nil] direction:UIPageViewControllerNavigationDirectionForward | UIPageViewControllerNavigationDirectionReverse animated:NO completion:nil];

[m_left hideGallery];
}

if(m_pageController.spineLocation == UIPageViewControllerSpineLocationMin)
{

[m_pageController setViewControllers:[NSArray arrayWithObject:[m_pageController.viewControllers objectAtIndex:0]] direction:UIPageViewControllerNavigationDirectionForward | UIPageViewControllerNavigationDirectionReverse animated:NO completion:nil];
}

[super viewWillAppear:animated];
}


- (UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation
{
if(UIInterfaceOrientationIsPortrait(orientation))
{
LeftPageController *left = [pageViewController.viewControllers objectAtIndex:0];
[pageViewController setViewControllers:[NSArray arrayWithObject:left] direction:UIPageViewControllerNavigationDirectionForward | UIPageViewControllerNavigationDirectionReverse animated:NO completion:nil];

pageViewController.doubleSided = NO;

return UIPageViewControllerSpineLocationMin;
}

if(UIInterfaceOrientationIsLandscape(orientation))
{
LeftPageController *left;
RightPageController *right;
if(pageViewController.viewControllers.count > 0)
{
left = [pageViewController.viewControllers objectAtIndex:0];
right = [m_storyboard instantiateViewControllerWithIdentifier:@"rightPage"];
[right loadView];
}

[pageViewController setViewControllers:[NSArray arrayWithObjects:left,right, nil] direction:UIPageViewControllerNavigationDirectionForward | UIPageViewControllerNavigationDirectionReverse animated:NO completion:nil];

return UIPageViewControllerSpineLocationMid;
}

return UIPageViewControllerSpineLocationMin;
}

我知道我做了两次同样的事情,但这就是我在这种情况下愿意付出的代价。为什么在调用 之后pageViewController:spineLocationForInterfaceOrientation: 并且为Landscape设置两个 View Controller ,当SecondVC处于非事件状态时, 中的m_pageController中只有一个ViewController View 将出现:激活 SecondVC 后对我来说仍然是个谜。

关于containers - UIPageViewController 在容器 View Controller 中的旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10137426/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com