2012-07-05 61 views
1

我試着檢查當前呈現的視圖控制器的UIInterfaceOrientation,並出於某種原因它總是返回風景。UIViewController的UIInterfaceOrientation

UIInterfaceOrientation currentOrientation = (UIInterfaceOrientation)[[UIApplication sharedApplication] statusBarOrientation]; 
      if (currentOrientation == UIInterfaceOrientationPortrait){ 
       self.scrollView_.colMargin = 50; 
      } else { 
       self.scrollView_.colMargin = 130; 
      } 

任何想法爲什麼?

回答

1

您可以檢查方向如下:

UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation]; 
    if(orientation == UIDeviceOrientationPortrait) ... 
+0

同樣的事情,這並未」 t也工作 – xonegirlz 2012-07-05 17:51:51

+1

你鎖定在橫向模式?你有設置可以旋轉的設備嗎?有一百萬的東西可以防止它,但我向你保證上面的代碼奇妙:) – bgoers 2012-07-05 17:59:58

+0

不,我不是..它被設置爲與肖像和風景旋轉 – xonegirlz 2012-07-05 21:09:55

0

鑄造不是魔術。狀態欄方向我使用的值不同於界面方向。改爲使用當前的視圖控制器self.interfaceOrientation屬性。

+0

它總是導致肖像 – xonegirlz 2012-07-05 17:42:51

1

從iOS的UIDevice類參考:

方向

返回設備的物理方向。 (只讀)

討論

除非取向通知已通過調用beginGeneratingDeviceOrientationNotifications啓用此屬性的值總是返回0。

所以,你需要做的是這樣的:

- (void) viewDidAppear:(BOOL)animated { 
[super viewDidAppear:animated]; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil]; 
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 

,然後不要忘記

[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications]; 

當你viewDidUnload