我想要实现的是,当用户按下按钮时,以编程方式模拟设备旋转到横向。 我的这段代码适用于iPhone设备,但不适用于iPad
let value = NSNumber(integerLiteral: UIDeviceOrientation.landscapeRight.rawValue)
UIDevice.current.setValue(value, forKey: "orientation")
ViewController.attemptRotationToDeviceOrientation()
有没有办法在iPad上实现相同的目标?
答案 0 :(得分:0)
答案 1 :(得分:-1)
您如何检测设备方向的变化?如果您使用
func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator)
进行检测,那么它将不会在iPad上被调用。您可以改用它
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
coordinator.animate(alongsideTransition: { (_) in
let orient = UIApplication.shared.statusBarOrientation
if orient.isPortrait {
// portrait now
} else if orient.isLandscape {
// landscape now
}
}, completion: nil)
super.viewWillTransition(to: size, with: coordinator)
}