iBhavik
iBhavik

Reputation: 647

UIInterfaceOrientation for current device not working

I want to know current orientation of my device.

if([[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortrait)
    {

    }

But this doent work

Upvotes: 0

Views: 2212

Answers (4)

Haritha
Haritha

Reputation: 1

The solution mentioned in the accepted answer worked for me.

Though my stmt 1 didnt get executed with the below logic

myOrientation = [[UIApplication sharedApplication] statusBarOrientation];
if(myOrientation == UIInterfaceOrientationPortrait) ||
    (myOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
    // stmt 1;
}

this one works for me right

if (UIDeviceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation )) 
{
    // stmt 1;
}

Upvotes: 0

iBhavik
iBhavik

Reputation: 647

I got it,

if (UIDeviceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation )) {

}

this work perfectly.

Upvotes: 1

giorashc
giorashc

Reputation: 13713

You should enable notifications of the device orientation like this :

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]

then you can query the current orientation

EDIT :

From the docs : "The value of this property always returns 0 unless orientation notifications have been enabled by calling beginGeneratingDeviceOrientationNotifications" (where 0 is UIDeviceOrientationUnknown)

Upvotes: 2

sonxurxo
sonxurxo

Reputation: 5718

The

[[UIDevice currentDevice] orientation]

will return one of

UIDeviceOrientationPortrait
UIDeviceOrientationPortraitUpsideDown
UIDeviceOrientationLandscapeLeft
UIDeviceOrientationLandscapeRight

constants. If you just want to check if its 'vertical' or 'horizontal', no matter if upside down or left-right, just check:

if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation]) {...}

If you want to detect orientation changes, override the

-(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {}

or

-(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {...}

methods.

Does this help you?

Upvotes: 0

Related Questions