我有一個UIWebView
,我想加載不同的網址取決於它的肖像或風景。我怎樣才能找出我目前的方向是viewWillAppear
?iOS:如何計算viewWillAppear中的當前方向?
回答
使用UIApplication的statusBarOrientation
屬性。如果使用設備方向,如果它是UIDeviceOrientationFaceUp
或UIDeviceOrientationFaceDown
,則可能會遇到問題。
例
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (UIInterfaceOrientationIsLandscape(orientation))
{
// Do something when in landscape
}
else
{
// Do something when in portrait
}
當應用程序加載時,它不知道它的當前定位 -
UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIDeviceOrientationPortrait) {
NSLog(@"portrait");// only works after a rotation, not on loading app
}
一旦你旋轉設備,你會得到正確的方向,但在加載應用程序時,不要改變方向,似乎使用[[UIDevice currentDevice] orientation]
不知道當前的方向。
所以,你需要做兩件事情 -
- 嘗試在plist文件設置應用程序的接受裝置定向
- 在你UIViewControllers,你將需要重寫
shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)
方法時,應用程序返回YES應該rotate:
我想你是誤會我的問題 - 我的方向改變罰款(我已經定義shouldAutorotate的話),但我想知道哪些變量,我可以檢查,看看我是什麼方向目前在。 – Gibson
檢查我更新的答案。你需要實現 - 'UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];' –
@Srikar我用'[[UIDevice currentDevice]方向]'爲此目的一次,並最終有一個錯誤在我的代碼時, UIDeviceOrientationFaceUp'或'UIDeviceOrientationFaceDown'。這是當我切換到使用'[UIApplication sharedApplication] .statusBarOrientation',這是'UIInterfaceOrientation'類型而不是'UIDeviceOrientation'。請注意,您不必顯示該代碼的狀態欄即可運行。我想在進行修改之前,我甚至在遇到面朝上/面朝下的錯誤時使用宏UIDeviceOrientationIsLandscape(deviceOrientation)。 – Sam
如果是相關的調用順序意味着你不能依靠interfaceOrientation
具有viewWillAppear
正確的值,那麼 - 假設括號t視圖控制器旋轉 - 最安全的事情可能是self.parentViewController.interfaceOrientation
。如果沒有,你可以嘗試從[[UIDevice currentDevice] orientation]
做出第一個假設,這可能並不總是100%(例如,當你的應用程序啓動時該設備平躺在桌子上),但可能會給你一個比總是更好的猜測假設肖像。
UIDeviceOrientation getCurrentOrientation() {
UIDevice *device = [UIDevice currentDevice];
[device beginGeneratingDeviceOrientationNotifications];
UIDeviceOrientation currentOrientation = device.orientation;
[device endGeneratingDeviceOrientationNotifications];
return currentOrientation;
}
這是給你轉換的UIDeviceOrientation
到UIInterfaceOrientation
。
這是唯一一個適合我的解決方案!謝謝! – maza23
- 1. 計算當前的android GPS行走方向的相反
- 2. MKMapView - 如何計算當前正在查看的平方英里
- 3. 如何計算在當前視圖
- 4. 如何計算當前指數?
- 5. 如何計算下一個當前行?
- 6. 如何在iOS 6中以編程方式獲取設備的當前方向?
- 7. 如何計算旋轉的方向?
- 8. 如何旋轉至當前的方向?
- 9. android - 如何獲取當前的方向?
- 10. iOS:如何根據當前方向翻轉圖像?
- 11. 64計算向前跳躍
- 12. 在SurfaceView的onMeasure()中,如何計算當前視圖的大小
- 13. viewWillAppear中VS viewDidLoad中IOS
- 14. 如何計算在javascript中選擇日期的當前日期?
- 15. 如何計算導航堆棧中當前VC的數量?
- 16. 一年有52.17周。如何計算C#中的當前週數?
- 17. 如何計算Django中的當前條紋?
- 18. 如何在運行時計算gmake中當前的Makefile位置
- 19. 如何計算UITextField中當前的插入符號位置?
- 20. 如何計算java中當前日期的下一個生日?
- 21. 如何計算播放中的當前聲音循環(0,30)
- 22. 如何計算iOS中的對數?
- 23. 如何計算ios中的年齡?
- 24. 如何計算iOS中的時間?
- 25. iOS SDK中當前位置的方位
- 26. 根據當前時間和地球上的位置計算太陽光方向
- 27. 計算方向性運動的算法
- 28. 如何計算屏幕朝向的方向
- 29. 如何計算從出生日期到當前日期的當前年齡?
- 30. 如何從python time.time()中計算當前月份()
在ios 9中已棄用 – trickster77777