2012-01-16 30 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); 
    } 
}