【问题标题】:xcode - How do I keep views locked into portrait mode, but still allow one view to rotate?xcode - 如何将视图锁定为纵向模式,但仍允许一个视图旋转?
【发布时间】:2013-02-27 11:22:03
【问题描述】:

我遇到了设备旋转问题。除了显示公司启动画面的 ONE 视图之外,我想将所有剩余的应用程序视图锁定为纵向显示。在项目设置中,支持的方向是纵向和横向。在“Company Splash”中,它工作正常,并且无论我如何旋转设备,视图旋转都被锁定到 LandscapeLeft。在所有其他视图中,当我将设备向左旋转时,视图会发生变化,而不是保持纵向显示。连开枪的方法都没有?如果我从项目中支持的方向中删除横向左侧,则会破坏“公司飞溅”视图。我尝试将shouldAutorotate 更改为NO,但这并没有帮助。试图通过发布here 的建议来解决问题,但这并没有帮助。如果我将以下代码放入我的 AppDelegate.m 中,所有内容都被锁定为纵向模式,并且“公司启动画面”在访问时崩溃。

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}

除了一个屏幕外,无论设备如何旋转,如何将视图锁定为纵向模式?

** 来自“Company Splash”视图的方法。再次,按预期工作。

-(NSInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeLeft;
}

** 来自所有其他视图的方法,当我不希望它们旋转时,它们会旋转出来

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // IOS5 Only returning that it should rotate to potrait
    return (interfaceOrientation == UIDeviceOrientationPortrait);
}

-(BOOL)shouldAutorotate
{
    // forcing the rotate IOS6 Only
    return YES;
}

-(NSInteger)supportedInterfaceOrientations
{
    // return number or enum IOS6 Only
    return UIInterfaceOrientationMaskPortrait;
}

我想这可能是因为 UITabBarController 是根控制器,而我在一个 ViewController 中?这些方法甚至没有触发?

【问题讨论】:

    标签: iphone xcode orientation portrait screen-rotation


    【解决方案1】:

    将观察者添加到要旋转的视图的 viewDidLoad 方法中,如下所示:

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter]
     addObserver:self selector:@selector(orientationChanged:)
     name:UIDeviceOrientationDidChangeNotification
     object:[UIDevice currentDevice]];
    

    然后根据 orientationChanged 方法中的横向视图设置视图,如下所示:

    - (void) orientationChanged:(NSNotification *)note{
    UIDevice * device = [UIDevice currentDevice];
    switch(device.orientation)
    {
        case UIDeviceOrientationPortrait:
    
            break;
        case UIDeviceOrientationPortraitUpsideDown:
    
            break;
        case UIDeviceOrientationLandscapeLeft:
    
            break;
        case UIDeviceOrientationLandscapeRight:
    
            break;
    
        default:
            break;
      };
    }
    

    【讨论】:

    • 对不起,我花了这么长时间才回复。我在箱子里放什么代码来强制设备进行一定的旋转?
    • 只需让您的应用程序仅在纵向模式下运行,并在需要旋转的视图的 viewDidLoad 方法中添加我上面提到的观察者,然后将orientationChanged 方法也添加到该类.当用户旋转视图时,您将收到通知。所以你可以通过代码设置横向模式的视图
    • 并不是我遇到的问题,但这个答案确实有效。 :)
    • @Ushan87 你能帮我回答我的这个问题吗?这里是我的问题的链接:stackoverflow.com/questions/20603996/…
    【解决方案2】:

    将此方法添加到您希望锁定在某个方向的视图控制器中。

    -(NSUInteger)supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskLandscape;
    }
    

    只需更改为您想要的方向(以上将其锁定为横向)

    【讨论】:

      【解决方案3】:

      也许你应该允许所有方向,并在每个类中锁定纵向方向,除了公司飞溅

      - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
      {
          if (interfaceOrientation == UIInterfaceOrientationPortrait) {
          return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
          } else {
          return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
          }
      }
      

      【讨论】:

      • 我已经试过了,但没有效果。我想可能是因为 UITabBarController 是根控制器,而我在 ViewController 中?这些方法甚至没有触发?
      • 有点明白发生了什么。将 rootViewController 设置为 tabcontroller 会导致这些方法仅在 tabControllerClass.m 中触发,而不是在各种 tabViewController.m 方法中触发。但是,现在我遇到了一个问题,即当方法在 tabcontroller 级别触发时,我试图确定我正在查看哪个选项卡。必须有一种更优雅的方式来处理这个问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-30
      • 1970-01-01
      相关资源
      最近更新 更多