【问题标题】:First UIView in Portrait orientation and second in Landscape纵向的第一个 UIView 和横向的第二个 UIView
【发布时间】:2013-05-06 13:23:20
【问题描述】:

我有一个有 2 个视图控制器的应用程序。第一个视图控制器应该是纵向的,没关系,但是当我加载第二个视图控制器时,我无法将应用程序方向设置为横向......问题出在 iOS 6 上。

我已经尝试了我在 SO 上找到的所有内容。

viewWillAppearviewDidLoad 上使用[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];

还有:

- (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;
}

【问题讨论】:

标签: iphone ios objective-c screen-orientation uiinterfaceorientation


【解决方案1】:

2nd View Controller'sviewDidLoad方法中的这些代码添加到transformviewlandscape中:

[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;
}

编辑:为radian添加这些macro

#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)

【讨论】:

    【解决方案2】:

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

     -(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 将自动管理方向。

    【讨论】:

    • 也尝试了这个转到项目设置的摘要部分并禁用支持的界面方向。
    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:
      -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
      {
             return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
      }
      
      -(BOOL)shouldAutorotate
      {
           return NO;
      }
      
      - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
      {
          return UIInterfaceOrientationLandscapeRight;
      }
      
      -(NSInteger)supportedInterfaceOrientations
      {
          return UIInterfaceOrientationMaskLandscapeRight;
      }
      

      您没有在方法 shouldAutorotate 中返回 NO,请将现有代码替换为 this 。 希望对你有帮助。

      【讨论】:

        【解决方案5】:

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

        这篇文章中清楚地解释了自动旋转- Autorotation in iOS

        【讨论】:

          猜你喜欢
          • 2012-03-29
          • 1970-01-01
          • 1970-01-01
          • 2014-06-03
          • 1970-01-01
          • 1970-01-01
          • 2014-09-16
          • 2012-09-25
          • 1970-01-01
          相关资源
          最近更新 更多