Device Orientation not getting.

爷,独闯天下 提交于 2019-12-11 03:26:15

问题


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

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.


回答1:


Try this.

UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;

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

EDIT : Use UIInterfaceOrientationIsLandscape and UIInterfaceOrientationIsPortrait method to find orientation mode




回答2:


try this

 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 other orientation call back methods this one called before rotation is going to occur - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration and this one just after second one - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation, for example

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

for more information see docs

hope this helps u .. :)



来源:https://stackoverflow.com/questions/25661792/device-orientation-not-getting

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!