CosmicMind / Material

A UI/UX framework for creating beautiful applications.
http://cosmicmind.com
MIT License
12k stars 1.26k forks source link

Set width of navigation drawer (storyboards) #147

Closed jossiwolf closed 8 years ago

jossiwolf commented 8 years ago

How can I set the width of a navigation drawer? I'm not creating it programatically, but via storyboards.

daniel-jonathan commented 8 years ago

Through Storyboards, I don't believe it will work. You could however use Storyboards for your ViewControllers, and then you would only need to set the SideNav's left or right width programmatically.

You would use these lines:

sideNavigationViewController?.setLeftViewWidth(300, hidden: true, animated: false)
sideNavigationViewController?.setRightViewWidth(300, hidden: true, animated: false)

In order for those lines to work correctly, it would need to be done in the viewWillAppear method of your ViewController, or after the instantiation of the SideNav in the AppDelegate.

Here are two examples:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Get view controllers from storyboard
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let mainViewController = storyboard.instantiateViewControllerWithIdentifier("MainViewController") as! MainViewController
let sideViewController = storyboard.instantiateViewControllerWithIdentifier("SideViewController") as! SideViewController
let sideNavigationViewController: SideNavigationViewController = SideNavigationViewController(mainViewController: mainViewController, leftViewController: sideViewController)
sideNavigationViewController.setLeftViewWidth(300, hidden: true, animated: false)

// Configure the window with the SideNavigationViewController as the root view controller
window = UIWindow(frame: UIScreen.mainScreen().bounds)
window?.rootViewController = sideNavigationViewController
window?.makeKeyAndVisible()
return true
}

// or

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
sideNavigationViewController?.setLeftViewWidth(view.bounds.width - 88, hidden: true, animated: false)
}

The reason for placing it in the viewWillAppear, if you choose that route, is because of the race-condition which would occur when instantiating the views and placing them within the SideNav.init method. Basically, the SideNav, won't exist at that time.

Hope this works for you :)

daniel-jonathan commented 8 years ago

If you have any further questions, please reopen or create a new issue. All the best :)

jossiwolf commented 8 years ago

Thank you!