0

我创建了两个相同的故事板,除了一个包含纵向视图和一个包含横向视图。

我不想使用自动调整大小的蒙版,因为某些视图的布局在纵向和横向之间完全变化。过去我在代码中手动移动了视图上的控件,但这次我采用了一种更简单的方法:)

我找到的最接近的解决方案来自 'benyboariu' - Storyboardsorientation support for xCode 4.2?

这是我正在使用的代码,它在 iOS 5.x 上运行良好,但在 iOS 6.x 上运行良好。

- (void)updateLandscapeView
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;

if (deviceOrientation == UIDeviceOrientationUnknown)
{
    if ([[UIScreen mainScreen] bounds].size.height > [[UIScreen mainScreen] bounds].size.width)
    {
        deviceOrientation = UIDeviceOrientationPortrait;
        self.appDelegate.isShowingLandscapeView = NO;
    }
    else
    {
        deviceOrientation = UIDeviceOrientationLandscapeLeft;
        self.appDelegate.isShowingLandscapeView = YES;
    }
}

if (UIDeviceOrientationIsLandscape(deviceOrientation) && !self.appDelegate.isShowingLandscapeView)
{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard-Landscape" bundle:[NSBundle mainBundle]];
    UIViewController * landscape = [storyboard instantiateViewControllerWithIdentifier:@"RootViewController-Landscape"];
    self.appDelegate.isShowingLandscapeView = YES;
    [UIView transitionWithView:landscape.view duration:0 options:UIViewAnimationOptionTransitionCrossDissolve|UIViewAnimationCurveEaseIn animations:^{
        self.view = landscape.view;
    } completion:NULL];
}
else if (UIDeviceOrientationIsPortrait(deviceOrientation) && self.appDelegate.isShowingLandscapeView)
{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard-Portrait" bundle:[NSBundle mainBundle]];
    UIViewController * portrait = [storyboard instantiateViewControllerWithIdentifier:@"RootViewController"];
    self.appDelegate.isShowingLandscapeView = NO;
    [UIView transitionWithView:portrait.view duration:0 options:UIViewAnimationOptionTransitionCrossDissolve|UIViewAnimationCurveEaseIn animations:^{
        self.view = portrait.view;
    } completion:NULL];
}
}

我在 iOS 6.x 上调试时遇到的错误是:

由于未捕获的异常“UIViewControllerHierarchyInconsistency”而终止应用程序,原因:“一个视图一次最多只能与一个视图控制器相关联!查看 UIView: 0x108276b0; 帧 = (0 0; 568 268); 自动调整大小 = RM+BM;动画={位置=CABasicAnimation:0x10815c60;bounds=CABasicAnimation: 0x1082a5e0; }; layer = CALayer: 0x10827710 与 RootViewController: 0x10821f10 相关联。在将此视图与 RootViewController 关联之前清除此关联:0x961a150。

我通常将视图控制器与 NIB 断开链接以修复此类错误,但无法通过情节提要执行此操作。

有人有什么想法吗?

4

2 回答 2

0

将您的肖像视图控制器更改为普通视图(将其从视图控制器中取出)。现在您应该能够将它实例化为 UIView 并进行转换,而不必担心它与多个视图控制器相关联。

于 2012-12-11T19:02:39.450 回答
0

好吧,在尝试了各种方法来尝试解决这个问题之后,我想出了这个适用于 iOS 5.x 和 6.x 的解决方案。

基本上,问题似乎是因为导航控制器,所以,而不是做

self.view = landscape.view;

或者

self.view = portrait.view;

您需要替换导航控制器堆栈中包含的所有视图控制器。

我在下面的代码中所做的是创建导航控制器堆栈中所有视图控制器的 NSMutableArray。然后循环每个视图控制器并获取视图标签以确定需要替换哪个视图。然后我只需将视图控制器替换为横向或纵向版本,然后将导航控制器视图控制器数组设置为修改后的数组,从而替换所有视图控制器。

if (UIDeviceOrientationIsLandscape(deviceOrientation) && !self.appDelegate.isShowingLandscapeView)
{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard-Landscape" bundle:[NSBundle mainBundle]];
    SearchViewController * landscape = [storyboard instantiateViewControllerWithIdentifier:@"SearchViewController-Landscape"];
    self.appDelegate.isShowingLandscapeView = YES;
    [UIView transitionWithView:landscape.view duration:0 options:UIViewAnimationOptionTransitionCrossDissolve|UIViewAnimationCurveEaseIn animations:^{
        NSMutableArray * viewControllers = [NSMutableArray arrayWithArray:[self.navigationController viewControllers]];
        if (viewControllers && [viewControllers count] > 0)
        {
            for (NSUInteger index = 0; index < [viewControllers count]; index++)
            {
                if ([[[viewControllers objectAtIndex:index] view] tag] == 1000)
                {
                    RootViewController * root = [storyboard instantiateViewControllerWithIdentifier:@"RootViewController-Landscape"];
                    [viewControllers replaceObjectAtIndex:index withObject:root];
                }
                else if ([[[viewControllers objectAtIndex:index] view] tag] == 2000)
                {
                    [viewControllers replaceObjectAtIndex:index withObject:landscape];
                }
            }

            [self.navigationController setViewControllers:viewControllers];
        }
    } completion:NULL];
}
else if (UIDeviceOrientationIsPortrait(deviceOrientation) && self.appDelegate.isShowingLandscapeView)
{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard-Portrait" bundle:[NSBundle mainBundle]];
    SearchViewController * portrait = [storyboard instantiateViewControllerWithIdentifier:@"SearchViewController"];
    self.appDelegate.isShowingLandscapeView = NO;
    [UIView transitionWithView:portrait.view duration:0 options:UIViewAnimationOptionTransitionCrossDissolve|UIViewAnimationCurveEaseIn animations:^{
        NSMutableArray * viewControllers = [NSMutableArray arrayWithArray:[self.navigationController viewControllers]];
        if (viewControllers && [viewControllers count] > 0)
        {
            for (NSUInteger index = 0; index < [viewControllers count]; index++)
            {
                if ([[[viewControllers objectAtIndex:index] view] tag] == 1000)
                {
                    RootViewController * root = [storyboard instantiateViewControllerWithIdentifier:@"RootViewController"];
                    [viewControllers replaceObjectAtIndex:index withObject:root];
                }
                else if ([[[viewControllers objectAtIndex:index] view] tag] == 2000)
                {
                    [viewControllers replaceObjectAtIndex:index withObject:portrait];
                }
            }

            [self.navigationController setViewControllers:viewControllers];
        }
    } completion:NULL];
}

我已经扩展了代码以展示如何在导航控制器中的嵌套视图上使用它。如果您正在使用根视图控制器,您显然不需要遍历所有视图控制器,只需替换索引 0 处的视图控制器。

希望这对某人有帮助!

于 2012-12-12T08:38:12.187 回答