首先,你应该设置的KYDrawerController
让它成为你的应用程序的根视图控制器,我会建议申报drawerController
作为一个实例变量在AppDelegate.swift
:
var drawerController:KYDrawerController?
因此,在didFinishLaunchingWithOptions
方法,你可以实现:
let appDelegate = UIApplication.shared.delegate as! AppDelegate
// let's say that you got your first view controller from the "Main" storyboard:
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
// asssume that controller would be "HomeViewController"
let homeVC = mainStoryboard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
let navigation:UINavigationController = UINavigationController(rootViewController: homeVC)
appDelegate.drawerController = KYDrawerController(drawerDirection: .left , drawerWidth: 280)
appDelegate.drawerController.mainViewController = navigation
appDelegate.window?.rootViewController = appDelegate.drawerController
通过执行上面的代码,你应该能够变化t他抽屉的状态下从应用anywhere
打开,如下:
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.drawerController!.setDrawerState(.opened, animated: true)
为什么你正在使用KYDrawerController? –
试试这个例子https://www.appcoda.com/ios-programming-sidebar-navigation-menu/ –