19

a noob question here.

i detect the orientation with:

UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];

all is fine and dandy and I reposition my text fields and labels according to the reported orientation with

if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown)

and else{} for everything else

the problem that i only recently discovered is when the UIDevice reports UIDeviceOrientationFaceUp or UIDeviceOrientationFaceDown. how do I deal with this situation ? how do I know whether UIDeviceOrientationFaceUp and UIDeviceOrientationFaceDown is happening in Portrait or Landscape ? I know that the device is facing up or down, but I don't know if I should reposition everything to Portrait or Landscape.

thank you!

3 Answers 3

33

Apple recommends against using device orientation for view layout. Instead, each view controller has an interfaceOrientation property, and UIApplication has a statusBarOrientation property, both of which will return the current interface orientation, which is suitable for view layout.

To monitor for changes, there are UIViewController methods like willRotateToInterfaceOrientation:duration: that will be called and notifications such as UIApplicationWillChangeStatusBarOrientationNotification that will be posted when an interface orientation change occurs.

3
  • This is the correct answer, just refactoring the UIDevice orientation response (as the blog suggests) isn't as good as listening to the interfaceOrientation of the UIViewController. Nice work.
    – Jessedc
    Apr 27, 2011 at 3:43
  • I like this answer better as well. caprica13, could you mark this one as correct? Dec 18, 2011 at 0:02
  • 1
    There are cases where one needs to layout a view based on the device orientation rather than the UI orientation. Look at the native camera app or Instagram's camera screen for example - the containers remain fixed with the UI orientation, while the icons rotate with the device orientation.
    – Pwner
    Jun 23, 2014 at 22:44
0

A bit late, hopefully it would help some one.

For iOS 6.0 and later:

1) Set this code during your View Setup to receive rotation notification:

  // Set Listener to receive orientation notifications from the device
  [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
  [[NSNotificationCenter defaultCenter] addObserver:self
                                           selector:@selector(detectOrientation)
                                               name:UIDeviceOrientationDidChangeNotification
                                             object:nil];

2) Set this code to catch the Notification after rotation:

-(void)detectOrientation{      
  switch ([[UIDevice currentDevice] orientation]) {
    case UIDeviceOrientationFaceDown:{
      NSLog(@"UIDeviceOrientationFaceDown");
      break;
    }
    case UIDeviceOrientationFaceUp:{
      NSLog(@"UIDeviceOrientationFaceUp");
      break;
    }
    case UIDeviceOrientationLandscapeLeft:{
      NSLog(@"UIDeviceOrientationLandscapeLeft");
      break;
    }
    case UIDeviceOrientationLandscapeRight:{
      NSLog(@"UIDeviceOrientationLandscapeRight");
      break;
    }
    case UIDeviceOrientationPortrait:{
      NSLog(@"UIDeviceOrientationPortrait");
      break;
    }
    case UIDeviceOrientationPortraitUpsideDown:{
      NSLog(@"UIDeviceOrientationPortraitUpsideDown");
      break;
    }
    case UIDeviceOrientationUnknown:{
      NSLog(@"UIDeviceOrientationUnknown");
      break;
    }
    default:{
      break;
    }
  }

}
-1

I have the same problems with

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

This method generate notifications for 7 possible device positional states:

UIDeviceOrientationPortrait
UIDeviceOrientationPortraitUpsideDown
UIDeviceOrientationLandscapeLeft
UIDeviceOrientationLandscapeRight
UIDeviceOrientationFaceUp
UIDeviceOrientationFaceDown
UIDeviceOrientationUnknown

But I only need the first 4, well this is my approach to solve this problem (with code included): How to handle UIDeviceOrientation for manage views layouts

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.