willTransition 函数在真实设备上不起作用 (swift 4)
willTransition function not working on a real device (swift 4)
我是 iOS 和 swift 的新手,希望大家能帮帮我。我正在做一个有侧边菜单的项目。当设备从横向转向纵向或从纵向转向横向时,我想让侧边菜单消失。我有这个功能:
override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
DispatchQueue.main.async{
//do something here
print("make side menu go away")
}
}
这在模拟器上有效,但当我在真实设备上测试时,它不起作用。有谁知道发生了什么事?或者有什么更好的方法来做到这一点?提前致谢!
改变UIViewConrollerTransitionCoordinator
至 UIViewControllerTransitionCoordinator
.
然后在iOS 12 iPhone 6s 上测试,调用了方法。
编辑
你应该使用这个方法
func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator)
或者你可以这样使用:
NotificationCenter.default.addObserver(self, selector: #selector(didRotate), name: UIDevice.orientationDidChangeNotification, object: nil)
@objc func didRotate() {
// Do something here
}
我是 iOS 和 swift 的新手,希望大家能帮帮我。我正在做一个有侧边菜单的项目。当设备从横向转向纵向或从纵向转向横向时,我想让侧边菜单消失。我有这个功能:
override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
DispatchQueue.main.async{
//do something here
print("make side menu go away")
}
}
这在模拟器上有效,但当我在真实设备上测试时,它不起作用。有谁知道发生了什么事?或者有什么更好的方法来做到这一点?提前致谢!
改变UIViewConrollerTransitionCoordinator
至 UIViewControllerTransitionCoordinator
.
然后在iOS 12 iPhone 6s 上测试,调用了方法。
编辑
你应该使用这个方法
func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator)
或者你可以这样使用:
NotificationCenter.default.addObserver(self, selector: #selector(didRotate), name: UIDevice.orientationDidChangeNotification, object: nil)
@objc func didRotate() {
// Do something here
}