我正在創建一個iPhone應用程序,其中包含帶有文本標籤的圖標。我希望手機旋轉到橫向模式時隱藏標籤,因爲它們沒有足夠的空間。什麼是最簡單的方法來做到這一點?手機處於橫向模式時隱藏標籤
0
A
回答
1
您可以先在viewDidLoad中添加一個NSNotification以瞭解設備的方向更改。
NSNotificationCenter.defaultCenter().addObserver(self, selector: "rotated", name: UIDeviceOrientationDidChangeNotification, object: nil)
這將調用函數「旋轉」時,設備知道它的方向改變了,那麼你只需要創建一個函數,並把我們的代碼裏面。
func rotated()
{
if(UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation))
{
print("landscape")
label.hidden = true
}
if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation))
{
print("Portrait")
label.hidden = false
}
}
1
得到,如果你想進行動畫處理的變化(例如淡出的標籤,或其他一些動畫),實際上你可以做到這一點與通過重寫viewWillTransitionToSize
方法旋轉同步例如
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
coordinator.animateAlongsideTransition({ (UIViewControllerTransitionCoordinatorContext) -> Void in
let orient = UIApplication.sharedApplication().statusBarOrientation
switch orient {
case .Portrait:
println("Portrait")
// Show the label here...
default:
println("Anything But Portrait e.g. probably landscape")
// Hide the label here...
}
}, completion: { (UIViewControllerTransitionCoordinatorContext) -> Void in
println("rotation completed")
})
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
}
以上內容來自於以下的答案必須採取的代碼示例:https://stackoverflow.com/a/28958796/994976
0
目標C版
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration
{
if (UIInterfaceOrientationIsPortrait(orientation))
{
// Show the label here...
}
else
{
// Hide the label here...
}
}
斯威夫特版本
override func willAnimateRotationToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval)
{
if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation))
{
// Show the label here...
}
else
{
// Hide the label here...
}
}
相關問題
- 1. 如何在橫向模式下隱藏標籤圖標?
- 2. 在橫向模式下隱藏標籤欄
- 3. 如果手機處於橫向模式,Android相機預覽方向不正確
- 4. 在橫向模式下隱藏字段
- 5. 安卓相機處於橫向模式,但用於縱向模式
- 6. 隱藏Android的標題欄,僅在橫向模式下
- 7. 隱藏全景標題在橫向模式[WP7]
- 8. 橫向隱藏UITabBar
- 9. 強制UIViewController處於橫向模式iOS7
- 10. 在橫向模式下隱藏狀態欄時的問題iphone
- 11. Android。 Mashmallow。相機預覽始終處於橫向模式
- 12. pushViewController: - 當設備處於橫向模式時,如何以縱向模式顯示?
- 13. iOS - 當前處於橫向模式時如何強制縱向模式
- 14. 如何在Ionic 3中以編程方式隱藏橫向模式下的標題和標籤?
- 15. 如何在方向更改爲橫向模式時隱藏Safari的工具欄?
- 16. 當設備處於橫向模式時,MPAndroidChart PieChartRenderer崩潰
- 17. UISplitViewController不會在橫向模式下隱藏masterViewController
- 18. 滾動型隱藏TextView的文本在橫向模式
- 19. 在Android佈局中隱藏橫向模式的字段
- 20. 以橫向模式在Android上隱藏URL地址欄
- 21. Handler.postDelayed當手機處於待機模式時不會調用
- 22. 爲什麼手機處於橫向模式時,ImageView上的捕獲圖像消失了?
- 23. 隱藏標籤
- 24. 隱藏標籤
- 25. 隱藏標籤
- 26. 隱藏用於手機的內容
- 27. 橫向模式下android手機的設備寬度?
- 28. 橫向模式手機上的小型圖庫傳送帶
- 29. 如何讓手機導航菜單以橫向模式顯示?
- 30. 在黑莓手機中禁用橫向模式
完美工作,謝謝! –
不客氣:) – Lee