我对iOS很新。我已经能够成功实现材料按钮和卡片视图,但是如何在故事板上完成侧抽屉控制器对我来说没有意义。你能帮助讲故事板上的示例代码吗?
寻找一个带有导航按钮的简单View Controller,打开sidedrawercontroller。
答案 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()