我需要在我的UIViewController的viewWillAppear方法中獲取接口方向。我所做的是:獲取設備接口方向viewWillAppear
if (self.interfaceOrientation == UIInterfaceOrientationPortrait)
NSLog(@"Portrait");
else
NSLog(@"Landscape");
當風景,我總是在viewWillAppear中和景觀在viewDidAppear越來越肖像。爲什麼?
所以,我嘗試使用[[UIApplication sharedApplication] statusBarOrientation],但是也發生了同樣的情況。我也試着在這裏張貼在堆棧溢出另一種解決方案:
UIDevice* device = [UIDevice currentDevice];
[device beginGeneratingDeviceOrientationNotifications];
UIDeviceOrientation currentOrientation = device.orientation;
[device endGeneratingDeviceOrientationNotifications];
這似乎返回正確的方向,但我現在據我知道剩下的映射UIDeviceOrientation到UIInterfaceOrientation,這是不可能的問題(正面朝下不能映射)。
此問題的任何解決方案?
如果您可以支持iOS 5+,最好在-viewWillLayoutSubviews方法中執行此項工作。如果您的應用程序剛剛完成加載,則可以在設置視圖框架並且窗口已完成定向之前調用-viewWillAppear。如果你需要在iOS 4中的信息,你真的需要等待-viewDidAppear被調用。 –
如果您使用的是導航控制器,那麼在viewWillAppear中檢查self.navigationController.interfaceOrientation如何? –
@JasonCoco這對我來說非常合適。爲iOS4找到解決方案也很有趣,但這很好。 –