ShouldAutorotate not working with controllar swift 2 navigation

My application uses several libraries. Basically KYDrawerController . I want my application to use only orientation orientation, but for one ViewControllar I need both landscapes and Potrait.

I have a search for any solution on the Internet. each solution was a subclass of the NavigationControllar and Override ShouldAutoRotate methods. but not of them worked for me.

and here is a rough overview of the entire StoryBoard

the gray color of the View is the KYDrawerControllar view that libry uses to work as a NavigationDrawer, such as Android.

I created a custom class for the Control Controllar and subclassed it to the required ViewControllar Navigation Controllar.

here is the code

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. } */ 

}

here is the 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 } } 

and I use StoryBoard segues to represent ViewControllars.

please help me with this.

+2
source share
1 answer

Here is my way:

  • in appdelegae.swift :

     var shouldSupportAllOrientation = false func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask { if (shouldSupportAllOrientation == true){ return UIInterfaceOrientationMask.All } return UIInterfaceOrientationMask.Portrait } 
  • in your input view, which introduces all kind of orientation (change to support all orientation, here I use a button as an example):

     @IBAction func next(sender: UIButton) { let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate appdelegate.shouldSupportAllOrientation = true self.performSegueWithIdentifier("next", sender: self) } 
  • in your input view, which introduces the whole kind of orientation (change the orientation to support portrait only):

     override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate appdelegate.shouldSupportAllOrientation = false } 
  • Finally, you may find that this works on all iPhone and iPad ipad2 devices except iPad iPad iPad; you should check β€œfull screen required” in your general project information to make sure that all kinds of orientations can be included in the album.

+13
source

All Articles