Search code examples
iphoneiosuideviceorientation

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");
}

Solution

  • 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");
    }