设备方向没有得到。

时间:2014-09-04 09:25:37

标签: ios iphone ios7

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

- (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中编写了这段代码,当我运行我的应用程序时,没有任何条件成为现实, 任何人都可以帮助我为什么即使我以纵向模式运行我的应用程序也没有条件成真。 提前谢谢。

2 个答案:

答案 0 :(得分:2)

试试这个。

UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;

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

编辑:使用UIInterfaceOrientationIsLandscapeUIInterfaceOrientationIsPortrait方法查找orientation mode

答案 1 :(得分:1)

试试这个

 UIInterfaceOrientation  orientation = [[UIApplication sharedApplication] statusBarOrientation];
  if (UIDeviceOrientationIsLandscape(orientation))
  {
      NSLog(@"i am in landscape mode");
  }
 if (UIDeviceOrientationIsPortrait(orientation)){
     NSLog(@"i am in portrait mode");
 }

oky u hav其他方向回调方法,这个方法在旋转之前调用- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration并且这个方法恰好在第二个- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation之后调用,例如

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
   UIInterfaceOrientation  orientation = toInterfaceOrientation; //hear toInterfaceOrientation contains the which orientation is device is turning to 
   if (UIDeviceOrientationIsLandscape(orientation))
   {
       NSLog(@"i am in landscape mode");
   }
   if (UIDeviceOrientationIsPortrait(orientation)){
       NSLog(@"i am in portrait mode");
  }
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
   UIInterfaceOrientation  orientation = fromInterfaceOrientation; // hear fromInterfaceOrientation contains previous state of the orientation 
   if (UIDeviceOrientationIsLandscape(orientation))
   {
      NSLog(@"i am in landscape mode");
   }
   if (UIDeviceOrientationIsPortrait(orientation)){
      NSLog(@"i am in portrait mode");
   }
}

有关详细信息,请参阅docs

希望这有助于你.. :)