how to deal with UIDeviceOrientationFaceUp and UIDeviceOrientationFaceDown?
Asked Answered
P

3

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!

Postilion answered 12/12, 2010 at 18:36 Comment(0)
E
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.

Escutcheon answered 12/12, 2010 at 19:10 Comment(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.Tellurian
I like this answer better as well. caprica13, could you mark this one as correct?Cockleboat
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.Design
B
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;
    }
  }

}
Blameless answered 16/9, 2015 at 16:27 Comment(0)
C
-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

Churchyard answered 18/6, 2011 at 0:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.