如何创建侧抽屉控制器?

时间:2016-06-29 21:12:42

标签: cosmicmind

我对iOS很新。我已经能够成功实现材料按钮和卡片视图,但是如何在故事板上完成侧抽屉控制器对我来说没有意义。你能帮助讲故事板上的示例代码吗?

寻找一个带有导航按钮的简单View Controller,打开sidedrawercontroller。

1 个答案:

答案 0 :(得分:1)

根据我的经验,如果您想使用NavigationDrawerController,则必须以编程方式初始化NavigationDrawerController

但是,您仍然可以通过情节提要板布局根视图控制器和侧控制器,并确保在Interface Builder中为它们提供正确的Storyboard ID。

以下是 AppDelegate.swift

中的代码
let mainViewController         = instantiateViewControllerWithIdentifier("MainViewController") as! MainViewController        
let sideMenuViewController     = instantiateViewControllerWithIdentifier("SideMenuViewController") as! SideMenuViewController
let navigationController       = NavigationController(rootViewController: mainViewController, leftViewController: sideMenuViewController)        
let navigationDrawerController = NavigationDrawerController(rootViewController: navigationController)

window = UIWindow(frame: UIScreen.mainScreen().bounds)
window?.rootViewController = navigationDrawerController
window?.makeKeyAndVisible()

P.S。如果你想使用UINavigationController,只需嵌入" MainViewController"在故事板中使用UINavigationViewController,并为 UINavigationViewController 提供一个Storyboard ID。最后,将rootViewController设置为UINavigationController实例,而不是MainViewController

使用 Swift3 Material 2.0

更新了

func instantiateViewControllerWithIdentifier(identifier: String) -> UIViewController {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    return storyboard.instantiateViewController(withIdentifier: identifier)
}

let mainViewController = instantiateViewControllerWithIdentifier(identifier: "MainViewController") as! MainViewController
let sideMenuViewController = instantiateViewControllerWithIdentifier(identifier: "SideMenuViewController") as! SideMenuViewController
let navigationController = NavigationController(rootViewController: mainViewController)
let navigationDrawerViewController = NavigationDrawerController(rootViewController: navigationController, leftViewController: sideMenuViewController, rightViewController: nil)

window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = navigationDrawerViewController
window?.makeKeyAndVisible()