shouldAutorotate not working with navigation controllar swift 2

余生颓废 提交于 2019-12-24 00:44:04

问题


my app uses several libraryies. Bassically KYDrawerController. I want my App to use only potraite Orientation but for one ViewControllar i need both landscape and Potrait.

I have search about allmost every solution on the internet. every solution was to subclass the NavigationControllar and Override ShouldAutoRotate method. but non of them worked for me.

and here is the closer View of the entire StoryBoard

the gray color View is the KYDrawerControllar view which is an libry uses to work as NavigationDrawer like Android.

I have created a custom Class for Navigation Controllar and subclass it to the requierd ViewControllar's 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 am using StoryBoard segues to present ViewControllars.

please someone help me with this.


回答1:


Here is my way:

  1. in your appdelegae.swift:

    var shouldSupportAllOrientation = false
    func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
        if (shouldSupportAllOrientation == true){
            return UIInterfaceOrientationMask.All
        }
        return UIInterfaceOrientationMask.Portrait
    }
    
  2. in your entry view which enter the all orientation view (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)
    }
    
  3. in your entry view which enter the all orientation view (change the orientation to support only Portrait):

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        appdelegate.shouldSupportAllOrientation = false
    }
    
  4. finally, you may find this works on all iPhone device and iPad ipad2 except iPad air iPad pro; you should check the "requires full screen" in your project general info to ensure you all orientation view can enter landscape.



来源:https://stackoverflow.com/questions/34037274/shouldautorotate-not-working-with-navigation-controllar-swift-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!