第一个UIView是纵向,第二个是横向

时间:2013-05-06 04:36:23

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

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

我已经尝试过在SO上找到的所有东西。

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

还有:

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

5 个答案:

答案 0 :(得分:1)

2nd View Controller's viewDidLoad方法中的这些代码添加到transform view landscape

[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)

答案 1 :(得分: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将自动管理方向。

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

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

-(BOOL)shouldAutorotate
{
     return NO;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}

-(NSInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeRight;
}

您没有在方法shouldAutorotate中返回NO,用此替换现有代码。 希望它会对你有所帮助。

答案 4 :(得分:0)

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

此帖中明确解释了自动旋转 - Autorotation in iOS