5

I would like to be able to handle orientation changes in my current iPhone application. The problem is that I don't want the view of my controller to rotate or resize. When the device orientation changes, I just want one UI element to rotate 90 degrees on the screen.

Apple's camera application is a perfect example of this - when the device orientation changes, all of the buttons rotate, but the view itself does not rotate.

I suppose that I could just resize the view, move all of my elements around, and then animate the buttons, but I feel like there must be an easier way to do it.

Thanks in advance.

tnbeatty
  • 315
  • 1
  • 2
  • 9

2 Answers2

5

1) You can create 2 view hierarchies and change which is on screen in willRotateToInterfaceOrientation: see this question

2) Or you can handle all rotations yourself see this question

Community
  • 1
  • 1
Jiri
  • 2,206
  • 1
  • 22
  • 19
  • 1
    Option 2 was perfect. Thanks for pointing me in the right direction. I had trouble finding that one for some reason. I appreciate your post and sample code. Awesome!! – tnbeatty Jun 04 '12 at 14:23
  • @tnbeatty I made an edit to the referenced answer, because it handled some values of UIDeviceOrientation incorrectly – Jiri Jun 04 '12 at 14:43
2

You can use

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
if(interfaceOrientation==UIInterfaceOrientationPortrait || interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown)
    {
        //Change frames as per orientations
    }

    else
    {    
          //Change frames as per orientations
    }


    return NO;
    }

and based on the orientation manually set the frames of the UI elements you want to change. you can access all the orientation from Constants section of this doc.

EDIT: You can use NSNotification for the issue.

UIApplicationDidChangeStatusBarOrientationNotification

Posted when the orientation of the application's user interface changes.

The userInfo dictionary contains an NSNumber object that encapsulates a UIInterfaceOrientation value (see UIInterfaceOrientation). Use UIApplicationStatusBarOrientationUserInfoKey to access this value Availability

Available in iOS 2.0 and later.

Declared In UIApplication.h

Himanshu A Jadav
  • 2,286
  • 22
  • 34
  • This is a good thought - that's what I tried first. Problem is: Say the device starts in UIInterfaceOrientationPortrait. If the user turns the device to UIInterfaceOrientationLandscapeLeft, I can detect that change and then change the frames. If the user then rotates back to UIInterfaceOrientationPortrait, this method won't be called. – tnbeatty Jun 04 '12 at 13:43
  • Thats interesting, in my experiance, this routine gets called every rotation (true) that happens. – trumpetlicks Jun 04 '12 at 13:53
  • 1
    I should clarify: In my previous comment, -shouldAutorotateToInterfaceOrientation won't be called because the device orientation never actually gets changed. Thus, the phone thinks it is in Portrait and won't call this method when the device changes back to portrait. Does that make sense? – tnbeatty Jun 04 '12 at 13:55
  • I am totally agree with you. suggested another way please check edits. – Himanshu A Jadav Jun 04 '12 at 14:00
  • Unfortunately it does not. How is it that the actual DEVICE orientation NEVER changes, yet somehow it CHANGES BACK TO PORTRAIT? From what then? – trumpetlicks Jun 04 '12 at 14:04
  • trumpetlicks - give it a try. The interface orientation does not get changed because you return "NO" in the -shouldAutorotate... method. – tnbeatty Jun 04 '12 at 14:17