woutr_be
woutr_be

Reputation: 9732

UIInterfaceOrientation on launch

I'm not sure if this a bug or something I might do wrong, but when I launch my application in portrait mode without rotating the device and I run this code

if(UIInterfaceOrientationIsPortrait([[UIDevice currentDevice] orientation])) {
    NSLog(@"Portrait");
} else {
    NSLog(@"Landscape");
}

I return Landscape, after I rotate the device and check again it returns the correct value, so it seems that when you first launch the returned orientation is wrong. Is this a known issue?

EDIT:

Even when I run this code in my AppDelegate it's returning Landscape, even when I launch in Portrait mode

if(UIInterfaceOrientationIsPortrait([[UIDevice currentDevice] orientation])) {
    NSLog(@"Portrait");
} else {
    NSLog(@"Landscape");
}

Upvotes: 1

Views: 723

Answers (3)

Henry H Miao
Henry H Miao

Reputation: 3528

That's because at first launch it returns UIDeviceOrientationUnknown. It's neither portrait nor landscape. If you want to detect the user interface orientation, you should use

UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (UIInterfaceOrientationIsLandscape(orientation)) {
    NSLog(@"landscape");
}else{
    NSLog(@"portrait");
}

Upvotes: 3

Mohd Kalimullah Sheikh
Mohd Kalimullah Sheikh

Reputation: 363

this is the code to Portrait Mode Only

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}

Upvotes: -1

Gustaf Rosenblad
Gustaf Rosenblad

Reputation: 1922

In your Info.plist > Supported interface orientations drag the item "Portrait (bottom home button)" up over the other items.

Upvotes: 0

Related Questions