Ios 我希望一个Viewcontroller仅以横向模式显示
我目前正在开发一个移动应用程序,它应该只锁定在纵向模式。由于我可以在项目设置中执行此操作,所以这不是问题,但我希望一个Viewcontroller仅以横向模式显示 我尝试在项目设置中禁用纵向模式,并将这段代码添加到我的横向视图控制器(以及调用它的控制器,但具有纵向方向):Ios 我希望一个Viewcontroller仅以横向模式显示,ios,swift,ios10,Ios,Swift,Ios10,我目前正在开发一个移动应用程序,它应该只锁定在纵向模式。由于我可以在项目设置中执行此操作,所以这不是问题,但我希望一个Viewcontroller仅以横向模式显示 我尝试在项目设置中禁用纵向模式,并将这段代码添加到我的横向视图控制器(以及调用它的控制器,但具有纵向方向): 我现在面临的问题是,这个解决方案不是最优的。用户仍然可以在设备上旋转他的手指。如何修复此问题?尝试锁定自动旋转 override var shouldAutorotate : Bool { // Lock autor
我现在面临的问题是,这个解决方案不是最优的。用户仍然可以在设备上旋转他的手指。如何修复此问题?尝试锁定自动旋转
override var shouldAutorotate : Bool {
// Lock autorotate
return false
}
这在iOS10之前起作用 viewdidload:
UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue, forKey: "orientation")
在viewcontroller中还具有以下功能:
override var shouldAutorotate : Bool {
return true
}
override var supportedInterfaceOrientations : UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.landscapeRight
}
override var preferredInterfaceOrientationForPresentation : UIInterfaceOrientation {
return UIInterfaceOrientation.landscapeRight
}
下面的代码可以正常工作
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if let rootViewController = self.topViewControllerWithRootViewController(rootViewController: window?.rootViewController) {
if (rootViewController.responds(to: Selector(("canRotate")))){
// Unlock landscape view orientations for this view controller
return .allButUpsideDown
}
}
// Only allow portrait (standard behaviour)
return .portrait
}
private func topViewControllerWithRootViewController(rootViewController: UIViewController!) -> UIViewController? {
if (rootViewController == nil) { return nil }
if (rootViewController.isKind(of: UITabBarController.self)) {
return topViewControllerWithRootViewController(rootViewController: (rootViewController as! UITabBarController).selectedViewController)
} else if (rootViewController.isKind(of: UINavigationController.self)) {
return topViewControllerWithRootViewController(rootViewController: (rootViewController as! UINavigationController).visibleViewController)
} else if (rootViewController.presentedViewController != nil) {
return topViewControllerWithRootViewController(rootViewController: rootViewController.presentedViewController)
}
return rootViewController}} .
//Add the below method in ViewController .
@objc func canRotate() -> Void {}
我也有同样的问题。我的代码在iOS 9中运行得很好,但随后开始允许用户在iOS 10中轮换,如果他们尝试的话。参考:
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if let rootViewController = self.topViewControllerWithRootViewController(rootViewController: window?.rootViewController) {
if (rootViewController.responds(to: Selector(("canRotate")))){
// Unlock landscape view orientations for this view controller
return .allButUpsideDown
}
}
// Only allow portrait (standard behaviour)
return .portrait
}
private func topViewControllerWithRootViewController(rootViewController: UIViewController!) -> UIViewController? {
if (rootViewController == nil) { return nil }
if (rootViewController.isKind(of: UITabBarController.self)) {
return topViewControllerWithRootViewController(rootViewController: (rootViewController as! UITabBarController).selectedViewController)
} else if (rootViewController.isKind(of: UINavigationController.self)) {
return topViewControllerWithRootViewController(rootViewController: (rootViewController as! UINavigationController).visibleViewController)
} else if (rootViewController.presentedViewController != nil) {
return topViewControllerWithRootViewController(rootViewController: rootViewController.presentedViewController)
}
return rootViewController}} .
//Add the below method in ViewController .
@objc func canRotate() -> Void {}