【问题标题】:Portrait and Landscape mode in iOS6iOS6 中的纵向和横向模式
【发布时间】:2013-05-15 19:11:11
【问题描述】:

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

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

我该如何更改以及如何更改才能使其再次正常工作?

【问题讨论】:

标签: xcode ios6 landscape-portrait


【解决方案1】:

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

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

然后,对于只需要 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
{

}

编辑:

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

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

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

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

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

【讨论】:

  • 看起来是个不错的方法,但我很遗憾地说,无论我如何按下按钮、支持的界面方向,它在我的旧项目中都不起作用。在一个新项目中,它似乎也不起作用。想知道我在这里做错了什么。
  • 检查编辑的答案。实际上,我也遇到了类似的问题,该问题已得到解决。
  • 会测试你所说的。但与此同时,我得到了轮换,将这些行设置为委托 self.window.rootViewController = self.navigationController;并且旋转是根据“按钮选择”进行的,但是我还不明白如何让一些 ViewControllers 始终处于纵向状态
  • 是的,[self.window addSubview] 已弃用,不应使用。此外,如果您的视图嵌入到 UINavigationController 中,则不会调用该 UIViewController 的supportedOrientation。相反,supportedOrientation 只会为 UINavigationController 调用。因此,这是 UINavigationController 子类化的解决方法
  • 在你这样做之后,只需 - (NSUInteger)supportedInterfaceOrientations { return (UIInterfaceOrientationMaskPortrait);肖像模式的 UIViewController 内。并保持 shouldAutorotate 为 NO
【解决方案2】:

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 小时才发现,它是它不起作用的原因)。

【讨论】:

    【解决方案3】:

    不知道您的问题是否相似,但在我看来,状态栏的方向正确(横向)并且描绘了 UIViewController。 我在应用程序委托 application:didFinishLaunchingWithOptions:

    中更改了以下行
    //[window addSubview:navigationController.view];
    
    self.window.rootViewController = navigationController;
    

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

    【讨论】:

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