2017-04-25 42 views
0

我只是想寫代碼,只有當設備顛倒時,標籤「dave」纔不會被隱藏。現在,標籤「dave」以全部4個方向顯示。這是我的代碼。當方向改變時,效果不被應用(swift3)

import UIKit 

class ViewController: UIViewController { 

@IBOutlet var dave: UILabel! 
override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
    s() 

} 


func s() { 
    if UIDevice.current.orientation == UIDeviceOrientation.landscapeLeft { 
     dave.isHidden = true 




    } else if UIDevice.current.orientation == UIDeviceOrientation.landscapeRight { 
     dave.isHidden = true 


    } else if UIDevice.current.orientation == UIDeviceOrientation.portrait { 
     dave.isHidden = true 


    } else if UIDevice.current.orientation == UIDeviceOrientation.portraitUpsideDown { 
     dave.isHidden = false 
    }}} 
+0

你知道* viewDidLoad *被解僱**一次**,對嗎?不要在那裏調用你的「s」功能 - 當方向改變時調用它。 (是的,你也想檢查* viewWillAppear *中的初始方向。) – dfd

回答

1

當你的設備方向成爲變革則viewWillTransition此委託將閃光。所以在這個方法中調用你的S()函數而不是viewDidLoad

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { 
     s() 
} 

希望這會對你有幫助。

相關問題