5

将我的应用程序更新到 iOS6 标准时,纵向/横向消失了。当我使用 Xcode 3 构建时,我工作得很好。但是现在使用最新的 Xcode 和最新的 SDK,旋转消失了,它始终处于纵向模式。不管我在“支持的界面方向”中放了什么。而我之前用来旋转的代码似乎根本没有效果。我有这些台词。

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

我该如何改变以及如何改变才能让它再次工作?

4

3 回答 3

17

首先,在 AppDelegate 中,这样写。这是非常重要的

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

然后,对于只需要 PORTRAIT 模式的 UIViewControllers,编写这些函数

- (BOOL)shouldAutorotate
{
     return YES;
}

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

对于也需要 LANDSCAPE 的 UIViewController,将遮罩更改为全部。

- (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 请求方向支持。因此我们需要对它进行子类化。

于 2012-10-04T13:33:31.800 回答
6

ios6主要为纵向的app如何支持一个或多个横向控制器:

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)在控制器中,您可以从控制器返回,可以在横向旋转 - 这很重要,否则它们在从启用横向的 VC 返回时仍然是横向。

- (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;
}

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

于 2012-12-14T15:46:44.787 回答
1

不知道您的问题是否相似,但对我来说,状态栏的方向正确(横向)并且UIViewController被描绘了。我在应用程序委托应用程序中更改了以下行:didFinishLaunchingWithOptions:

//[window addSubview:navigationController.view];

self.window.rootViewController = navigationController;

Apple=> 这花了我一天半的时间才找到,而且花了很多钱!!!

于 2012-10-19T13:11:41.167 回答