iOS6接口方向已损坏

时间:2012-10-09 15:47:47

标签: orientation ios6 uiinterfaceorientation

只需将iOS 5应用更新到iOS 6,当然偶然发现了界面问题。我现在已经知道事情发生了变化,我甚至知道发生了什么变化,但我似乎无法独自管理它。

我的应用程序由几个viewcontrollers组成,它们被推送到一个navigationcontroller。所有的viewcontrollers都应该是横向的,除了一个应该是纵向的。在iOS 5中,一切工作正常,但现在纵向模式下的一个控制器也以横向模式显示,当然也会失真。

我的iOS 5代码看起来像这样:

横向模式下的Viewcontroller:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
return (interfaceOrientation==UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation==UIInterfaceOrientationLandscapeRight);
}

纵向模式下的Viewcontroller:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
return (interfaceOrientation==UIInterfaceOrientationPortrait) || (interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown);
}

现在我发现了我实施的iOS 6中的变化

AppDelegate :(允许所有方向,如在plist中)

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
return UIInterfaceOrientationMaskLandscapeLeft|UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}

横向模式下的Viewcontroller :(将方向限制为横向模式)

- (NSUInteger) supportedInterfaceOrientations{
return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}

- (BOOL)shouldAutorotate{ 
return YES;
}  

纵向模式下的Viewcontroller :(将方向限制为纵向模式)

- (NSUInteger) supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { 
return UIInterfaceOrientationPortrait | UIInterfaceOrientationPortraitUpsideDown;
}

- (BOOL)shouldAutorotate { 
return YES;
}

根据我的理解,这应该有效,但事实是它比以前更有效。 首先,横向模式视图控制器不会保持横向模式,它们可以在所有方向上自由旋转,这当然不是我想要的。其次,纵向模式下的viewcontroller将崩溃

Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason: 'preferredInterfaceOrientationForPresentation must return a supported interface orientation!'

经过一些试验和错误后,我似乎只能通过plist文件/ appdelegate将所有控制器锁定到横向模式,这当然会强制纵向模式控制器进入横向模式。另一方面,我可以设置纵向模式控制器处于正确的方向,但这也将横向模式控制器旋转为纵向模式。我似乎无法兼顾两者。

有什么想法吗?

2 个答案:

答案 0 :(得分:7)

好的我修好了。

我为UINavigationController创建了一个类别

@implementation UINavigationController (Rotation_IOS6)

-(BOOL)shouldAutorotate
{
return [self.topViewController shouldAutorotate];
}

-(NSUInteger)supportedInterfaceOrientations
{
return [self.topViewController supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.topViewController preferredInterfaceOrientationForPresentation];
}

@end

将视图控制器的值转发到navigationcontroller ...

我还必须用presentViewController替换presentModalViewController,因为第一个现在已弃用。

顺便说一下,是的,我的appdelegate中有一个拼写错误...这是正确的。

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return UIInterfaceOrientationMaskLandscapeLeft|UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskPortraitUpsideDown;
}

答案 1 :(得分:0)

刚刚观察到你的UIInterfaceOrientationMaskLandscapeLeft在appdelegate中写了两次LandscapeRight失踪 如果它只是错字并且仍然面临纠正后的问题

我发现只有rootViewControllers - (BOOL)shouldAutorotate;被叫。这里你的rootViewController是NavigationController。对于推送的viewController,这不会被调用,所以你只能从navControllers方法决定对推送视图的方向支持。 以下为我的navControllers工作推送viewController
覆盖UINavigationControllers方法

- (BOOL)shouldAutorotate;
- (NSUInteger) supportedInterfaceOrientations;

对于你来说,代码将是这样的

@implementaion UINavigationController(Rotation)

- (BOOL)shouldAutorotate
{
return YES;
}

- (NSUInteger) supportedInterfaceOrientations
{
    NSArray *viewsArray = [self viewController]//stack of pushed controller

    NSUInteger orientationToReturn = assignAllMask;

    for(UIViewController *temp in  viewsArray)
    {
        if([temp isKindOfClass:MyOnlyLandscapeViewController] == YES )
        {
            orientationToReturn = assignLandscapeMask;
            break;
        }
        else if([temp isKindOfClass:MyOnlyPortraitViewController] == YES )
        {
            orientationToReturn = assignPortraitMask;
            break;
        }
    }

    return orientationToReturn;
}

@end