当方向改变时,效果不适用(swift3)
When Orientation is changed effects are not applied (swift3)
我只是想写代码,只有当设备倒置时标签 "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
}}}
当您的设备方向发生变化时,viewWillTransition
该委托将触发。所以在这个方法中调用你的 S()
函数而不是 viewDidLoad
.
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
s()
}
希望对您有所帮助。
我只是想写代码,只有当设备倒置时标签 "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
}}}
当您的设备方向发生变化时,viewWillTransition
该委托将触发。所以在这个方法中调用你的 S()
函数而不是 viewDidLoad
.
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
s()
}
希望对您有所帮助。