首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS6接口方向已损坏

iOS6接口方向已损坏
EN

Stack Overflow用户
提问于 2012-10-09 23:47:47
回答 2查看 6.3K关注 0票数 4

刚刚不得不将iOS 5应用程序更新到iOS 6,当然也遇到了界面方向问题。我现在已经知道事情发生了变化,我甚至知道是什么发生了变化,但我似乎不能独自管理它。

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

我的iOS 5代码是这样的:

横向模式下的Viewcontroller:

代码语言:javascript
复制
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
return (interfaceOrientation==UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation==UIInterfaceOrientationLandscapeRight);
}

纵向模式下的Viewcontroller:

代码语言:javascript
复制
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
return (interfaceOrientation==UIInterfaceOrientationPortrait) || (interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown);
}

现在我了解了我在iOS 6中实现的更改

AppDelegate:(允许所有方向,就像plist一样)

代码语言:javascript
复制
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
return UIInterfaceOrientationMaskLandscapeLeft|UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}

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

代码语言:javascript
复制
- (NSUInteger) supportedInterfaceOrientations{
return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight
}

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

- (BOOL)shouldAutorotate{ 
return YES;
}  

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

代码语言:javascript
复制
- (NSUInteger) supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}

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

- (BOOL)shouldAutorotate { 
return YES;
}

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

代码语言:javascript
复制
Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason: 'preferredInterfaceOrientationForPresentation must return a supported interface orientation!'

经过一些尝试和错误之后,我似乎只能通过plist文件/appdelegate将所有控制器锁定到横向模式,这当然会迫使纵向模式控制器也进入横向模式。另一方面,我可以设法让纵向模式控制器处于正确的方向,但这也会将横向模式控制器旋转到纵向模式。我似乎不能让两个都起作用。

有什么想法吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-10-11 18:50:04

好吧,我把它修好了。

我为UINavigationController做了一个分类

代码语言:javascript
复制
@implementation UINavigationController (Rotation_IOS6)

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

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

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

@end

要将视图控制器的值转发到导航控制器...

我还不得不用presentViewController替换presentModalViewController,因为第一个已经被弃用了。

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

代码语言:javascript
复制
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return UIInterfaceOrientationMaskLandscapeLeft|UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskPortraitUpsideDown;
}
票数 7
EN

Stack Overflow用户

发布于 2012-10-11 03:45:33

我观察到,在appdelegate中编写了两次的UIInterfaceOrientationMaskLandscapeLeft中,如果只是拼写错误,则缺少LandscapeRight,更正后仍面临问题

我只找到了rootViewControllers - (BOOL)shouldAutorotate;被调用。这里你的rootViewController是NavigationController。对于推送的viewController,这不会被调用,所以你必须决定是否只支持来自navControllers方法的推送视图。下面的内容对我的navControllers pushed很有效

重写UINavigationControllers方法

代码语言:javascript
复制
- (BOOL)shouldAutorotate;
- (NSUInteger) supportedInterfaceOrientations;

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

代码语言:javascript
复制
@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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12803762

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档