Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I make this code skeleton for dealing with wanted &amp; unwanted devices orientations, in my case i want to ignore the UIDeviceOrientationUnknown, UIDeviceOrientationFaceUp and UIDeviceOrientationFaceDown, caching the last allowed orientation. This code deals with iPhone and iPad devices and can be useful for you.</p> <pre><code>- (void)modXibFromRotation { UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; NSString *device = [[UIDevice currentDevice]localizedModel]; UIInterfaceOrientation cachedOrientation = [self interfaceOrientation]; if ([device isEqualToString:@"iPad"]) { if (orientation == UIDeviceOrientationUnknown || orientation == UIDeviceOrientationFaceUp || orientation == UIDeviceOrientationFaceDown) { orientation = (UIDeviceOrientation)cachedOrientation; } if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) { /* Your code */ } if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown) { /* Your code */ } } if ([device isEqualToString:@"iPhone"] || [device isEqualToString:@"iPod"]) { if (orientation == UIDeviceOrientationUnknown || orientation == UIDeviceOrientationFaceUp || orientation == UIDeviceOrientationFaceDown) { orientation = (UIDeviceOrientation)cachedOrientation; } if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) { /* Your code */ } if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown) { /* Your code */ } } } </code></pre> <p>Yo have to call this method from two places in your view controller:</p> <p>Fist begin generating device Orientation Notifications in your App delegate:</p> <pre><code>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. //**** ADD THIS CODE ***** [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; // Add the main view controller's view to the window and display. self.window.rootViewController = self.mainViewController; [self.window makeKeyAndVisible]; return YES; </code></pre> <p>}</p> <p>then listen for device Orientation Notifications in your view controller:</p> <pre><code>- (void)viewDidLoad { [notificationCent addObserver:self selector:@selector(modXibFromRotation) name:UIDeviceOrientationDidChangeNotification object:nil]; } </code></pre> <p>Finally call the modXibFromRotation method from:</p> <pre><code>- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear]; [self modXibFromRotation]; } </code></pre> <p>This will call the check orientation method before the view is shown.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload