iOS6中的竖屏和横屏模式

5

在将我的应用程序更新到iOS6标准时,纵向/横向旋转功能消失了。在使用Xcode 3构建时,它运行得非常完美。但现在使用最新的Xcode和最新的SDK后,旋转功能不再存在,而且始终处于纵向模式。无论我在“支持的界面方向”中放置什么内容都没有效果。而我以前用来实现旋转的代码似乎根本没有任何作用。 我曾经使用过以下代码:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    switch (toInterfaceOrientation) {
        case UIInterfaceOrientationPortrait:
        case UIInterfaceOrientationLandscapeLeft:
        case UIInterfaceOrientationLandscapeRight:
            return YES;
        default:
            return NO;
    }
}

我该如何更改,以及需要更改什么才能使其重新工作?


试试这个。对我有用:http://stackoverflow.com/questions/10096672/uiwindows-root-view-controller-does-not-rotate-to-landscape-at-app-launch - Homer Wang
3个回答

17

首先,在AppDelegate中写下这段代码。这非常重要。

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
     return (UIInterfaceOrientationMaskAll);
}

如果您只需要 UIViewController 的 PORTRAIT 模式,请编写以下函数

- (BOOL)shouldAutorotate
{
     return YES;
}

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

对于需要横屏的UIViewController,将掩码更改为All。

- (NSUInteger)supportedInterfaceOrientations
{
    return (UIInterfaceOrientationMaskAllButUpsideDown);
    //OR return (UIInterfaceOrientationMaskAll);
}

如果您想在方向更改时进行一些更改,则可以使用此函数。

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{

}

编辑:

很多情况取决于您的UIViewController嵌入在哪个控制器中。

例如,如果它位于UINavigationController内部,则可能需要子类化该UINavigationController以覆盖方向方法,如下所示。

子类化UINavigationController(层次结构中的顶级视图控制器将控制方向)。将其设置为self.window.rootViewController。

 - (BOOL)shouldAutorotate
 {
     return self.topViewController.shouldAutorotate;
 }
 - (NSUInteger)supportedInterfaceOrientations
 {
     return self.topViewController.supportedInterfaceOrientations;
 }

iOS 6开始,UINavigationController不再向其UIVIewControllers询问方向支持。因此我们需要对其进行子类化。


看起来是一个不错的方法,但很抱歉我必须说无论我如何按按钮、支持的界面方向与否,在我的旧项目中它都无法正常工作。而在一个新项目中似乎也不起作用。不知道我在这里做错了什么。 - Lars -
检查编辑后的答案。实际上,我也遇到了类似的问题,已经解决了。 - mayuur
会测试你所说的内容。但与此同时,我已经成功让旋转功能在委托中工作了,通过设置以下代码:self.window.rootViewController = self.navigationController; 现在根据“按钮选择”,旋转已经可以正常工作。但是我还不明白如何让一些视图控制器始终处于纵向模式。 - Lars -
我们可以使用分类(category)来替代继承 UINavigationController。 - user501836
一个UITabBarController是我的rootViewController。在遵循这里的建议之后,我的视图仍然会从纵向旋转。 - Grymjack
显示剩余4条评论

6
如何在 iOS6 中支持主要为纵向的应用程序中的一个或多个横向控制器:
1)在AppDelegate中
 - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
    {
        UINavigationController* ns = (UINavigationController*)self.window.rootViewController;
        if (ns) {
            UIViewController* vc = [ns visibleViewController];
//by this UIViewController that needs landscape is identified
            if ([vc respondsToSelector:@selector(needIos6Landscape)])
                return [vc supportedInterfaceOrientations];

        }
        return UIInterfaceOrientationMaskPortrait; //return default value
    }

2) 在需要横屏(或者竖屏+横屏等)的UIView控制器中:

//flag method
-(void)needIos6Landscape {
}
- (BOOL)shouldAutorotate
{
    return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

3) 在控制器中,你可以从控制器中返回到可以在横屏下旋转的控制器,这很重要,否则它们会保持在横屏状态下,无法正常返回到横屏可用的视图控制器。

- (BOOL)shouldAutorotate
{
    return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

4) (可能不是必要的,但肯定有用) - 子类化你正在使用的导航控制器,并添加:

- (BOOL)shouldAutorotate
{
    return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
    UIViewController* vc = [self visibleViewController];
    if (vc) {
        if ([vc respondsToSelector:@selector(needIos6Landscape)]) {
             return [vc supportedInterfaceOrientations];
        }
    }
    return UIInterfaceOrientationMaskPortrait;
}

重要的一步是从您的应用程序仅请求方向控制器,因为在控制器之间转换期间,会有一些系统控制器作为根返回不正确的值(这让我花了2个小时才发现,原因是它没有工作)。


1

不知道你的问题是否类似,但在我的情况下,状态栏是正确定位的(横屏),UIViewController也被呈现出来了。 我在应用程序委托中更改了以下行:application:didFinishLaunchingWithOptions:

//[window addSubview:navigationController.view];

self.window.rootViewController = navigationController;

苹果=> 这让我花了一天半的时间才找到,还花了很多钱!!!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接