shouldAutorotate 不适用于导航控制器 swift 2

shouldAutorotate not working with navigation controllar swift 2

我的应用程序使用了多个库。基本上 KYDrawerController。我希望我的应用程序仅使用 potraite 方向,但对于一个 ViewControllar,我需要横向和 Potrait。

我在互联网上搜索了几乎所有的解决方案。每个解决方案都是子类化 NavigationControllar 并重写 ShouldAutoRotate 方法。但没有一个对我有用。

here is the closer View of the entire StoryBoard

灰色视图是 KYDrawerControllar 视图,它是一个像 Android.

一样用作 NavigationDrawer 的库

我为 Navigation Controllar 创建了一个自定义 Class 并将其子类化为所需的 ViewControllar 的 Navigation Controllar。

这是代码

class SettingsNavigation: UINavigationController {

override func viewDidLoad() {
    super.viewDidLoad()
   NSLog("visibleControllar", self.visibleViewController!.debugDescription)
    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func shouldAutorotate() -> Bool {
    //return self.visibleViewController!.shouldAutorotate()
    return false
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.Portrait
}

override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
    return UIInterfaceOrientation.Portrait
}



/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

}

这里是 ViewControllar

class Settings: UIViewController {

// MARK: - Outlets
@IBOutlet var profileImage: UIImageView!
@IBOutlet var profileName: UILabel!
@IBOutlet var profileRegistrationType: UILabel!
@IBOutlet var logOutButton: FUIButton!
@IBOutlet var subscriptionType: UILabel!
@IBOutlet var subscriptionRegistrationType: UILabel!
@IBOutlet var upgradeSubscriptionButton: FUIButton!



override func shouldAutorotate() -> Bool {
   /* if (UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeLeft ||
        UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeRight ||
        UIDevice.currentDevice().orientation == UIDeviceOrientation.Unknown) {
            return false;
    }
    else {
        return true;
    }*/

    return false

}

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.Portrait
} }

我正在使用 StoryBoard segues 来呈现 ViewControllars。

请有人帮我解决这个问题。

这是我的方法:

  1. 在你的 appdelegae.swift:

    var shouldSupportAllOrientation = false
    func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
        if (shouldSupportAllOrientation == true){
            return UIInterfaceOrientationMask.All
        }
        return UIInterfaceOrientationMask.Portrait
    }
    
  2. 在你进入全向视图的入口视图中(改为支持全向,这里我以按钮为例):

    @IBAction func next(sender: UIButton) {
        let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        appdelegate.shouldSupportAllOrientation = true
        self.performSegueWithIdentifier("next", sender: self)
    }
    
  3. 在进入全方向视图的入口视图中(更改方向以仅支持纵向):

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        appdelegate.shouldSupportAllOrientation = false
    }
    
  4. 最后,您可能会发现这适用于所有 iPhone 设备和 iPad ipad2 除了 iPad air iPad pro;你应该在你的项目一般信息中检查 "requires full screen" 以确保你所有的方向视图都可以进入横向。