我只是想编写代码,只有当设备颠倒时才会隐藏标签“dave”。现在标签“dave”以所有4个方向显示。这是我的代码。
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 :(得分:1)
当您的设备方向发生变化时,viewWillTransition
此代表将会触发。因此,请在此方法中调用S()
函数,而不是viewDidLoad
。
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
s()
}
希望这会对你有所帮助。