shouldAutorotate不使用导航控制器swift 2

时间:2015-12-02 07:41:44

标签: uinavigationcontroller swift2 ios9 xcode7 screen-orientation

我的应用程序使用了几个库。贝斯特KYDrawerController。我希望我的应用程序仅使用potraite Orientation但是对于一个ViewControllar我需要横向和Potrait。

我搜索了互联网上的所有解决方案。每个解决方案都是子类化NavigationControllar和Override ShouldAutoRotate方法。但他们没有为我工作。

here is the closer View of the entire StoryBoard

灰色视图是KYDrawerControllar视图,它是一个libry用于像Android这样的NavigationDrawer。

我为导航控制器创建了一个自定义类,并将其子类化为需要的ViewControllar的导航控制器。

这是代码

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 个答案:

答案 0 :(得分:13)

这是我的方式:

  1. appdelegae.swift

    var shouldSupportAllOrientation = false
    func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
        if (shouldSupportAllOrientation == true){
            return UIInterfaceOrientationMask.All
        }
        return UIInterfaceOrientationMask.Portrait
    }
    
  2. 输入全部方向视图的输入视图中的
  3. (更改为支持所有方向,此处我以按钮为例):

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

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        appdelegate.shouldSupportAllOrientation = false
    }
    
  6. 最后,您可能会发现这适用于所有iPhone设备和iPad ipad2,除了iPad air iPad pro;您应该检查项目常规信息中的“需要全屏”,以确保您可以进入所有方向视图。