设备方向没有得到。

问题描述:

目前我正在研究IOS 7,我的问题是我无法在ViewDidLoad方法中获取当前设备的方向。

Currently i am working on IOS 7 and my problem is i am not able to get orientation of current device in ViewDidLoad method.

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return (UIInterfaceOrientationMaskAll);

}
- (void)viewDidLoad
{
    [super viewDidLoad];

    if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)){
        NSLog(@"i am in landscape mode");
    }
    if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation)){
        NSLog(@"i am in portrait mode");
    }
}

我在 FirstViewController.m ,当我运行我的应用程序时,没有任何条件成为现实,
任何人都可以帮助我为什么没有条件变为真,即使我在纵向模式下运行我的应用程序。
提前致谢。

I write this code in my FirstViewController.m,and when i run my application no condition become true out of them, can anybody help me why no condition become true even i run my application in portrait mode. Thanks in advance.

试试这个。

UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;

if(UIInterfaceOrientationIsLandscape(orientation)) {
    //Landscape mode
}
else
{
    //Portrait mode
}

编辑:使用 UIInterfaceOrientationIsLandscape UIInterfaceOrientationIsPortrait 查找方向模式的方法