首页 文章

iOS6中的纵向和横向模式

提问于
浏览
5

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

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

我如何更改以及如何更改以使其再次运行?

3 回答

  • 1

    首先,在AppDelegate中,写下这个 . 这是非常重要的

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

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

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

    对于也需要LANDSCAPE的UIViewControllers,将掩码更改为All .

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

    现在,如果要在Orientation更改时进行一些更改,请使用此功能 .

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

    EDIT :

    很大程度上取决于你的UIViewController嵌入哪个控制器 .

    例如,如果它在UINavigationController中,那么您可能需要将UINavigationController子类化为覆盖这样的方向方法 .

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

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

    从iOS 6开始,UINavigationController不会要求其UIVIewControllers提供方向支持 . 因此我们需要将其子类化 .

  • 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)在控制器中,您可以从控制器返回,可以在横向上旋转 - 这很重要,否则他们会在从启用风景的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小时才发现,这是它的原因没工作) .

  • 17

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

    //[window addSubview:navigationController.view];
    
    self.window.rootViewController = navigationController;
    

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

相关问题