1

I have an app which has 2 view controllers. The first viewcontroller should be in portrait and it's ok, but when I'm loading the second view controller, I could not make the app orientation to be landscape... The problem is with iOS 6.

I have tried everything I found on SO.

Using [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO]; on viewWillAppear and viewDidLoad,

and also:

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

 -(BOOL)shouldAutorotate
  {
return YES;
  }

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
 {
return UIInterfaceOrientationLandscapeRight;
 }

-(NSInteger)supportedInterfaceOrientations{

return UIInterfaceOrientationMaskLandscapeRight;
}
4

5 回答 5

1

将这些代码添加到方法2nd View Controller's viewDidLoad中:transform viewlandscape

[self rotateController:self degrees:-90];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:NO];
//set it according to 3.5 and 4.0 inch screen in landscape mode
[self.view setBounds:CGRectMake(0, 0, 480, 320)]; 

添加rotateController方法:

 -(void) rotateController:(UIViewController *)controller degrees:(NSInteger)aDgrees
{
  UIScreen *screen = [UIScreen mainScreen];
  if(aDgrees>0)
    controller.view.bounds = CGRectMake(0, 0, screen.bounds.size.height, screen.bounds.size.width);
  else
  {
    controller.view.bounds = CGRectMake(0, 0, screen.bounds.size.width, screen.bounds.size.height);
  }
  controller.view.transform = CGAffineTransformConcat(controller.view.transform, CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(aDgrees)));
}

现在在viewWillDisappear's方法中transform view进入protrait. 添加这些:

[self rotateController:self degrees:90];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
//set it according to 3.5 and 4.0 inch screen in protrait mode
[self.view setBounds:CGRectMake(0, 0, 320, 480)];

orientation如果不添加这些方法,您的方法也应该是这样的:

- (BOOL)shouldAutorotate
{    
  //make view landscape on start
  return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
  return UIInterfaceOrientationMaskLandscapeLeft;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
  return UIInterfaceOrientationLandscapeLeft;
}

编辑:添加macro这些radian

#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)
于 2013-05-06T05:05:58.533 回答
0

在 iOS 6 之前,即在 iOS 5 及更早版本中,应用程序和视图控制器的旋转由单独的视图控制器控制,而在 iOS 6 及更高版本中,负责旋转的视图控制器是容器视图控制器,例如 UINavigationController 和 UITabBarController。您在项目中使用什么作为 rootviewcontroller?

自动旋转在这篇文章中清楚地解释了iOS中的自动旋转

于 2013-05-06T05:23:20.620 回答
0

您可以在此视图控制器上添加此代码,您想要修复您的方向代码如下

 -(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
        {
            if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone)
        {
                  return UIInterfaceOrientationMaskPortrait;

            }

    }

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{

    if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone)
    {

        return (toInterfaceOrientation==UIInterfaceOrientationPortrait);
    }


}

第二个视图控制器你可能想要旋转所有方向然后不需要添加任何代码 ios6 将自动管理方向。

于 2013-05-06T04:49:04.773 回答
0

您需要在应用程序摘要-> 部署信息中选择横向模式。

此外,如果您使用 xib 文件来设计 UI,则需要将第二个视图控制器的方向设置为横向

于 2013-05-06T04:52:10.767 回答
0
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
       return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

-(BOOL)shouldAutorotate
{
     return NO;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}

-(NSInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeRight;
}

你没有在方法中返回 NO,shouldAutorotate用这个替换你现有的代码。希望它会帮助你。

于 2013-05-06T05:09:16.417 回答