禁用导航控制器中View Controller的旋转
首先,这不是重复。 我在SO上查看了与此相关的所有问题,但这些问题对我来说都不起作用。 希望这只是因为我是iOS开发的新手,但我怀疑在我的情况下这是不可能的。 我附加了一个带有视图控制器的图片,我想要禁用旋转。
我已经尝试过:对我要禁用旋转的视图控制器进行子类化,并使用shouldAutoRotate
方法将其设置为NO。 显然这不起作用,因为导航控制器决定了它的视图控制器是否可以旋转。 因此,我将UINavigationController
子类化,并在故事板中使用此类而不是默认的导航控制器。 在这个课程中,我将shouldAutoRotate
设置为NO。 它仍然无法正常工作。 真的不知道我做错了什么。
当我使用我的视图控制器类扩展根视图控制器并将shouldAutoRotate
设置为NO时,它会禁用整个应用程序的旋转…. 这不是我想要的。 我只希望为图片中圈出的视图控制器禁用旋转。
提前致谢!
添加AppDelegate.h
@property (nonatomic , assign) bool blockRotation;
AppDelegate.m
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { if (self.blockRotation) { return UIInterfaceOrientationMaskPortrait; } return UIInterfaceOrientationMaskAll; }
在视图控制器中,您要禁用旋转
- (void)viewDidLoad { [super viewDidLoad]; AppDelegate* shared=[UIApplication sharedApplication].delegate; shared.blockRotation=YES; } -(void)viewWillDisappear:(BOOL)animated{ AppDelegate* shared=[UIApplication sharedApplication].delegate; shared.blockRotation=NO; }
感谢@ozgur为我工作的修复程序。 我会’投票’,但显然我不够好(无论如何!)投票是否有效。 无论如何,这里是在Swift中:
在AppDelegate.swift中:
class AppDelegate: UIResponder, UIApplicationDelegate { var blockRotation: Bool = false func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int { if (self.blockRotation) { println("supportedInterfaceOrientations - PORTRAIT") return Int(UIInterfaceOrientationMask.Portrait.rawValue) } else { println("supportedInterfaceOrientations - ALL") return Int(UIInterfaceOrientationMask.All.rawValue) } }
在要阻止旋转的ViewController中,将UIApplicationDelegate添加到您的类中……
class LoginViewController: UIViewController, UITextFieldDelegate, UIApplicationDelegate {
然后创建对AppDelegate的引用…
var appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
在viewDidLoad中,设置appDelegate.blockRotation = true:
override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. appDelegate.blockRotation = true }
在viewWillAppear中,设置方向以强制设备进入所选方向(本例中为纵向):
override func viewWillAppear(animated: Bool) { let value = UIInterfaceOrientation.Portrait.rawValue UIDevice.currentDevice().setValue(value, forKey: "orientation") }
然后在viewWillDisappear或prepareForSegue中,设置appDelegate.blockRotation = false:
override func viewWillDisappear(animated: Bool) { appDelegate.blockRotation = false }
在阅读本网站上的其他解决方案数小时之后,这对我来说非常适合我(导航控制器中的多视图控制器)场景。 再次感谢@ ozgur – 如果可以,我会对你的答案进行投票!
快乐的小道!
对于那些使用Swift 2的人,您可以按照Andre的回答,但在第一步中,使用AppDelegate.swift中的以下代码:
class AppDelegate: UIResponder, UIApplicationDelegate { var blockRotation: Bool = false func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask { if (self.blockRotation) { print("supportedInterfaceOrientations - PORTRAIT") return UIInterfaceOrientationMask.Portrait } else { print("supportedInterfaceOrientations - ALL") return UIInterfaceOrientationMask.All } }
supportedInterfaceOrientationsForWindow:现在返回UIInterfaceOrientationMask而不是Int。
您必须检查您的根视图控制器是当前顶级控制器允许的旋转,并在supportedInterfaceOrientations方法中返回YES或NO。 所以它应该像你的根控制器中的以下代码( 使代码适应你的情况 ):
- (NSUInteger)supportedInterfaceOrientations { return [self.navigationController.topViewController supportedInterfaceOrientations]; }
然后在每个视图控制器中添加支持的接口方向,例如:
- (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; }
UINavigationController +旋转类别
当您使用导航控制器时, ViewController
SupportedInterfaceOrientaions
将无法将此类别添加到您的项目中,您将从viewController获取响应,而不是单独使用NavigationController
。
我的案例有3个视图控制器:
– 第一个视图控制器:肖像
– 第二视图控制器:横向右侧(具有导航控制器并由第一视图控制器呈现)
– 第三视图控制器:纵向(具有导航控制器并被第二视图控制器推动)
这是我在swift 3中的解决方案:
————————————
在AppDelegate :
– 添加此属性以保存您的设置
private var orientation: UIInterfaceOrientationMask = .portrait
– 然后创建一个为您的设备设置旋转的function:
func rotateScreen(orientation: UIInterfaceOrientationMask) { self.orientation = orientation var value = 0; if orientation == .landscapeRight { value = UIInterfaceOrientation.landscapeRight.rawValue }else { value = UIInterfaceOrientation.portrait.rawValue } UIDevice.current.setValue(value, forKey: "orientation") }
– 最后,实现支持定位方法:
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask { return self.orientation }
——————————–
然后你可以在显示目标视图控制器之前这样调用
(UIApplication.shared.delegate as! AppDelegate).rotateScreen(orientation: .landscapeRight)
例如,在我的情况下:当用户点击First的按钮来呈现第二个时,我会这样做
(UIApplication.shared.delegate as! AppDelegate).rotateScreen(orientation: .landscapeRight) self.present(navigation, animated: true, completion: nil)
第二个关闭按钮我会这样做
(UIApplication.shared.delegate as! AppDelegate).rotateScreen(orientation: .portrait) self.dismiss(animated: true, completion: nil)
就这样! 您永远不会介意使用导航控制器。 希望这可以帮助你们^ __ ^!