Search code examples
iphoneiosobjective-cscreen-orientationuiinterfaceorientation

First UIView in Portrait orientation and second in Landscape


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

Solution

  • Add these code in 2nd View Controller's viewDidLoad method to transform view into 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)]; 
    

    add rotateController method :

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

    Now in viewWillDisappear's method to transform view into protrait . Add these:

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

    Also your orientation methods should be like these if not added add these :

    - (BOOL)shouldAutorotate
    {    
      //make view landscape on start
      return NO;
    }
    
    - (NSUInteger)supportedInterfaceOrientations
    {
      return UIInterfaceOrientationMaskLandscapeLeft;
    }
    
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
    {
      return UIInterfaceOrientationLandscapeLeft;
    }
    

    EDIT : Add these macro for radian

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