質問

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");
}
役に立ちましたか?

解決

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

他のヒント

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

this is the code to Portrait Mode Only

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top