user2168907
user2168907

Reputation: 31

How to detect that device orientation is different from the interface orientation?

Initially a subclass of UINavigationController (Navigator) is a root controller and it supports all orientations. The subclass overrides supportedInterfaceOrientations and provides properties to set what orientation is supported.

The root view controller of Navigators navigation stack (subclass of UITableViewContreller) controls supported orientations (depending which view controller is on top of a stack). It sets Navigators orientation properties in the didSelectRowAtIndexPath override.

If a transition is made when a device is in different orientation (because current view does not support it and this is not a supposed way to interact) and new view supports that device orientation, the view remains in different orientation than the device orientation. Then one needs to rotate the device and move it back to bring a proper orientation.

This is if someone for some reason would hold a device in landscape mode in Contacts App, but suddenly one of it's subviews would support landscape and rotate automatically without rotating device to portrait and then landscape. The question is how to implement it?

Upvotes: 3

Views: 259

Answers (3)

Jeepston
Jeepston

Reputation: 1359

Add this method to your subclass of UINavigationController:

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
        return;

    if ([UIViewController respondsToSelector:@selector(attemptRotationToDeviceOrientation)]) {
        //present/dismiss viewcontroller in order to activate rotating.
        UIViewController *mVC = [[UIViewController alloc] init];
        [self presentModalViewController:mVC animated:NO];
        [self dismissModalViewControllerAnimated:NO];
    }
}

(Found it here on SF, but cannot find a link to that question)

Upvotes: 0

pdrcabrod
pdrcabrod

Reputation: 1477

I think this is want you want.

To get the device orientation:

[[UIDevice currentDevice] orientation];

To get the current orientation of the views showed:

[UIApplication sharedApplication].statusBarOrientation;

Upvotes: 0

Lokesh Chowdary
Lokesh Chowdary

Reputation: 736

Use this in every method:

if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) || 
    ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)) 
{       

} 
else 
{

}

or check the [[UIScreen mainScreen] bounds]

Upvotes: 1

Related Questions