通用应用中的IOS 6方向问题

5
在我的通用应用程序中,我需要处理iPhone和iPad的不同方向。对于iPad,我需要允许横向方向,而对于iPhone仅支持纵向方向。我首先返回了以下代码:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
        return UIInterfaceOrientationIsPortrait(interfaceOrientation);

    return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}

iOS 5中运行良好,但在iOS 6中,自动旋转方法根本没有触发。之后我更改了方法为:

-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationMaskPortrait;
}

即使这些方法在iOS 6中根本没有运行。

我的plist设置如下:

enter image description here

我需要处理iOS 5和iOS 6的两种方向[ iPhone - portrait,iPad - landscape]。请指导我如何解决此问题。


请参考我在stackoverflow上的回答:https://dev59.com/uWnWa4cB1Zd3GeqP3LS8 - Deepesh
除了被接受的答案之外,preferredInterfaceOrientationForPresentation 要求一个单一的方向,而你返回了一个掩码 (UIInterfaceOrientationMaskPortrait),而不是例如 UIInterfaceOrientationPortrait - Jesper
4个回答

3

你的rootviewcontroller是否是UINavigation controller或UITabbarcontroller?如果是,那么如果在你的视图控制器中调用这些方法,它们将不起作用。

因此,在这些容器视图控制器上创建一个Objective C类别并添加到你的项目中。

@implementation UINavigationController (autorotate)


 -(NSUInteger)supportedInterfaceOrientations
 {
   //make the check for iphone/ipad here

if(IPHONE)
    {
return UIInterfaceOrientationMaskPortrait;
    } 
else
    {
return UIInterfaceOrientationMaskLandscape;
    }

 }

 - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
 {
return UIInterfaceOrientationPortrait;
 }

 - (BOOL)shouldAutorotate
 {
return NO;
 }

2

将此方法添加到您的应用程序委托中,它可以100%正常工作。

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window  // iOS 6 autorotation fix
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return UIInterfaceOrientationPortraitUpsideDown;
    }
    else
        return  UIInterfaceOrientationMaskAll;
}

0
你需要的是以下内容:
#pragma mark - iOS 5.1 and under Rotation Methods

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation; {
  if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    return UIInterfaceOrientationIsPortrait(interfaceOrientation);
  }
  else{
    return UIInterfaceOrientationIsLandscape(interfaceOrientation);
  }
}

#pragma mark - iOS 6.0 and up Rotation Methods

- (BOOL)shouldAutorotate; {
  return YES;
}

- (NSUInteger)supportedInterfaceOrientations; {
  if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown);
  }
  else{
    return UIInterfaceOrientationMaskLandscape;
  }
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation; {
  if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    return UIInterfaceOrientationPortrait;
  }
  else{
    return UIInterfaceOrientationLandscapeLeft;
  }
}

你需要确保的是两个方面

首先,这必须在你的RootViewController中实现。如果你将UIViewController嵌入到UINavigationController(或UITabBarController)中,则必须创建一个自定义的UINavigationController(或自定义的UITabBarController)类来实现这些方法并使用它。

其次,你必须确保在你的Info.plist文件中支持这些方向。否则,系统将覆盖这些方法返回的值(注意:你也可以通过在目标摘要页面上单击按钮轻松更改这些值)。

希望能对你有所帮助!


0
这是Pradeep答案的Swift版本。您不需要为了实现您想要的功能(即仅针对iPhone关闭横向方向)而子类化所有导航控制器。
只需将以下代码添加到您的应用程序委托中即可:

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {
    if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
        return Int(UIInterfaceOrientationMask.Portrait.rawValue)
    } else {
        return Int(UIInterfaceOrientationMask.All.rawValue)
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接