【问题标题】:Make a UiViewController stay in portrait mode iOS6 VS iOS5使 UiViewController 保持纵向模式 iOS6 VS iOS5
【发布时间】:2012-10-10 06:10:08
【问题描述】:

我正在为 iOS5 和 iOS6 构建应用程序。我在 UINavigationController 中有这个 UIViewController,我希望它保持纵向模式。

该代码适用于 iOS5,但不适用于 iOS6。

// iOS5 rotation
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    if (interfaceOrientation == UIInterfaceOrientationPortrait)
        return YES;
    else
        return NO;
}

// iOS6 rotation
- (BOOL)shouldAutorotate
{

    return YES;


}
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;

}

这里有什么问题???在 SO 上,我发现了很多类似的问题,但通常答案对我不起作用。

编辑: 也许我不够精确,但我需要一个 SINGLE 视图控制器(我的应用程序的主页)来保持纵向模式而不是所有应用程序。

【问题讨论】:

    标签: ios5 ios6


    【解决方案1】:

    首先,很大程度上取决于您的 UIViewController 嵌入到哪个控制器中。

    例如,如果它在 UINavigationController 内部,那么您可能需要将该 UINavigationController 子类化以覆盖这样的方向方法。

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

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

    从 iOS 6 开始,UINavigationController 不会向其 UIVIewControllers 请求方向支持。因此我们需要对它进行子类化。

    更多

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

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

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

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

    现在,如果你想在方向改变时做一些改变,那就使用这个功能。

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

    非常重要

    在 AppDelegate 中,写下这个。这是非常重要的。

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

    如果您只想为所有视图控制器提供纵向模式,请应用纵向蒙版。即 UIInterfaceOrientationMaskPortrait

    否则,如果您希望某些 UIViewController 保持纵向,而其他 UIViewController 支持所有方向,则应用 ALL 蒙版。即 UIInterfaceOrientationMaskAll

    【讨论】:

      【解决方案2】:

      试试这个:

      -(BOOL)shouldAutorotate
      {
          return NO;
      }
      
      -(NSUInteger)supportedInterfaceOrientations
      {
          UIInterfaceOrientationMask orientationMask = UIInterfaceOrientationMaskPortrait;
          return orientationMask;
      }
      
      -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
      {   
         return (interfaceOrientation == UIInterfaceOrientationPortrait);
      }
      

      这应该只支持纵向模式。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多