0

I use following code I found in the web to rotate the screen to landscape mode. I don’t understand what they suppose to do. Specially the bounds it is setting. Can someone give some explanation what it is doing?

if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait)
{
    CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];

    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];


    UIScreen *screen = [UIScreen mainScreen];
    CGRect newBounds = CGRectMake(0, 0, screen.bounds.size.height, screen.bounds.size.width - statusBarFrame.size.height);



    self.navigationController.view.bounds = newBounds;
    self.navigationController.view.center = CGPointMake(newBounds.size.height / 2.0, newBounds.size.width / 2.0);
    self.navigationController.view.transform = CGAffineTransformConcat(self.navigationController.view.transform, CGAffineTransformMakeRotation(degreesToRadian(90)));
    self.navigationController.view.center = window.center;
}
Janaka
  • 2,505
  • 4
  • 33
  • 57
  • 3
    You'd be much better served letting the window and UIViewControllers apply the transformation themselves and then deal with any view changes you needs specifically by implementing the appropriate methods on your view controller. – Jason Coco Aug 19 '11 at 04:44
  • Based on our requirement we have to do it manually – Janaka Aug 19 '11 at 04:45
  • There's no good reason to manually apply a transform to a navigation controller. You will just end up shooting yourself in the foot. What it's doing, however, is applying a transform matrix to the view with a 90-degree rotation. If you do this, your bounds will be wrong (they will be too long for the new screen height and too narrow for the new screen width. The code above updates the bounds to fit the new screen, repositions the center to where the new logical center of the screen will be, applies the rotation around the view's center and then moves the center back to the center of the screen – Jason Coco Aug 19 '11 at 04:51
  • we are applying it to navigation controller's view right? not the navigation controller right? – Janaka Aug 19 '11 at 04:58
  • Yes, that's right. And the navigation controller will be very unhappy with you for doing so ;) – Jason Coco Aug 19 '11 at 05:03
  • the problem i had was in some of my screens i hide the navigation controller to get more space. when i do that the background of the screen started showing in the area where the navigation controller was if i relay on the automatic rotation. Because of that i use manual rotation – Janaka Aug 19 '11 at 05:08
  • In that case, use a variable height auto-resizing mask on your view and the nav controller will take care of it. If you can't do that, manually resize your view and reposition the few things you need inside of your view controller's rotation callbacks and all will be very well... – Jason Coco Aug 19 '11 at 05:11
  • @JasonCoco let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/2650/discussion-between-janaka-and-jason-coco) – Janaka Aug 19 '11 at 06:43

3 Answers3

0

Your rootView's size used to be (320, 480) for example, after rotating, you should set it to (480, 320) in order to fit the screen in landscape mode, that's why you need to change the bounds of your view.

Set the transform is making the view actually rotate 90 degrees.

UIKit is doing the similar things when automatically rotate for you.

xuzhe
  • 5,100
  • 22
  • 28
0

Hello Janaka,

         You will try this code.

         Take a look at the function `shouldAutorotateToInterfaceOrientation`: in the UIViewController class. This function returns YES if the orientations is supported by your UIView. If you return YES only to the landscape orientation, then the iPhone will automatically be put in that orientation.

The following code should do it. Put it in the UIViewController that controls the view that you want to put in landscape mode.

Use this Method. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return (interfaceOrientation == UIInterfaceOrientationLandscape); }

Try this link its definitely help you this

Community
  • 1
  • 1
Nikunj Jadav
  • 3,417
  • 7
  • 38
  • 54
  • I want the view to be permanently in landscape mode. I don’t want it to be change based on device. – Janaka Aug 19 '11 at 13:42
0
#define degreesToRadians(x) (M_PI * x / 180.0)

- (void)viewWillAppear:(BOOL)animated
{

    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];

    CGRect newBounds = CGRectMake(0, 0, 480, 320);
    self.navigationController.view.bounds = newBounds;
    self.navigationController.view.center = CGPointMake(newBounds.size.height / 2.0, newBounds.size.width / 2.0);

    self.navigationController.view.transform = CGAffineTransformMakeRotation(degreesToRadians(90));

    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.view.transform = CGAffineTransformIdentity;
    self.navigationController.view.transform = CGAffineTransformMakeRotation(degreesToRadians(0));
    self.navigationController.view.bounds = CGRectMake(0.0, 0.0, 320.0, 480.0);

    [super viewWillDisappear:animated];
}
Michal Zaborowski
  • 5,039
  • 36
  • 35