2012-01-16 31 views
1

我基本上运行此代码:iPad的定位在返回正确

UIInterfaceOrientation statusBarOrientation = [[UIApplication sharedApplication] statusBarOrientation]; 

if(statusBarOrientation == UIInterfaceOrientationPortrait){ 
    NSLog(@"orientation is portrait"); 
} 

不过,无论在模拟器实际方向或我的iPad,它是印刷“方向是纵向”。尝试NSLog statusBarOrientation作为%d也返回1,不管方向如何。

我坚持这是我的应用程序委托,我的视图控制器和我需要它的类,它是同样的事情。我的info.plist/target设置支持所有4种设备方向。

有没有人有一个明确界定方向,或为什么我的不工作?谢谢

回答

1

您可以注册通知的方向变化:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 
[[NSNotificationCenter defaultCenter] addObserver:self 
          selector:@selector(didRotate:) 
          name:@"UIDeviceOrientationDidChangeNotification" 
          object:nil]; 

- (void)didRotate:(NSNotification *)notification 
{ 
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; 

if ((orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight)) { 
     // DO STUFF 
} 
else if (orientation == UIDeviceOrientationPortrait) { 
      //DO MORE STUFF 
} 

} 
+0

谢谢。由于某些原因,这段代码实际上在视图加载上工作这将非常有帮助。谢谢 – user339946 2012-01-16 03:20:35

+0

没问题,很高兴我能帮上忙。 – 2012-01-16 03:29:10

0

目前您正在返回状态栏的方向。相反,获得设备的方向:[[UIDevice currentDevice] orientation]

+0

您好,我想这个代码,但它似乎并没有工作。 (UIDeviceOrientationIsLandscape([[UIDevice currentDevice] orientation])){NSLog(@“we are landscape”); } 谢谢 – user339946 2012-01-16 03:12:25

+0

你的viewController是否返回YES:'shouldAutorotateToInterfaceOrientation' – Evan 2012-01-16 16:02:42

3
,如果你不喜欢使用的通知的方位。然后下面的方法用得

这个例子的iPad只有横向的,纵向的iPhone的...

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
    { 
     if(interfaceOrientation==UIInterfaceOrientationLandscapeLeft || 
     interfaceOrientation==UIInterfaceOrientationLandscapeLeft) 
      return YES; 
     else 
      return NO; 
    } 
    else 
    { 
     return (interfaceOrientation == UIInterfaceOrientationPortrait); 
    } 
}