overshadow
overshadow

Reputation: 988

iOS Swift SlideMenuControllerSwift

I am learning to develop iOS and come across to this awesome library SlideMenuController. I am able to make use of this library to create awesome slide menu easily.

However, there are some problem that I am facing during implementing with this library.

Environment

In my project storyboard, I have 1 TabBarViewController and 2 ViewController which is under this TabBarViewController. I also have 2 independent ViewController which are LoginViewController and LeftMenuViewController.

I have below code in my AppDelegate.swift file

    ...
    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        let storyboard = UIStoryboard(name: "Main", bundle: nil)

        let mainViewController = storyboard.instantiateViewControllerWithIdentifier("TabBarViewController") as! TabBarViewController
        let leftViewController = storyboard.instantiateViewControllerWithIdentifier("LeftMenuViewController") as! LeftMenuViewController


        let slideMenuController = SlideMenuController(mainViewController: mainViewController, leftMenuViewController: leftViewController)

        SlideMenuOptions.contentViewScale = 1
        SlideMenuOptions.hideStatusBar = false;

        self.window?.rootViewController = slideMenuController
        self.window?.makeKeyAndVisible()

        return true
    }
    ...

Everything work perfectly if I don't need my login view as a initial view. I notice that this line of code self.window?.rootViewController = slideMenuController will always make sure that view in mainViewController to be the initial view when the app open.

How can I have my LoginView as the initial view when my app start up and the slide menu is still bind to the TabBarViewController and working properly?

I have try to move the code from AppDelegate.swift to viewDidLoad() function in TabBarViewController.swift but no luck. It is not working.

Need some guilds and help on this. Thanks.

Upvotes: 1

Views: 3546

Answers (1)

Ayoub Nouri
Ayoub Nouri

Reputation: 201

you can use this code instead of yours :

var window: UIWindow?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

         let storyboard = UIStoryboard(name: "Main", bundle: nil)

    let mainViewController = storyboard.instantiateViewControllerWithIdentifier("TabBarViewController") as! TabBarViewController
    let leftViewController = storyboard.instantiateViewControllerWithIdentifier("LeftMenuViewController") as! LeftMenuViewController
    let loginViewController = storyboard.instantiateViewControllerWithIdentifier("LoginViewController") as! LoginViewController

    let nvc: UINavigationController = UINavigationController(rootViewController: loginViewController)
    leftViewController.mainVC = nvc
    let slideMenuController = SlideMenuController(mainViewController: nvc, leftMenuViewController: leftViewController)

    SlideMenuOptions.contentViewScale = 1
    SlideMenuOptions.hideStatusBar = false;

    self.window?.rootViewController = slideMenuController
    self.window?.makeKeyAndVisible()

    return true
}

dont forget to add this variable in your LeftMenuViewController

var mainVC : UIViewController!

Upvotes: 3

Related Questions