swift4 使用DrawerController实现侧滑菜单功能的示例代码
本文介绍了swift4 使用DrawerController实现侧滑功能的示例代码,分享给大家,具体如下:
直接上图
安装
类库开源地址:https://github.com/sascha/DrawerController
可惜的是,它已经不维护了,很好用的一个侧滑实现
pod 'DrawerController'
新建侧滑视图
import UIKit // 这个类就是一个 UIViewController 可以在里面写任何你想写的东西 class LeftViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() self.title = "Left Menu" self.view.backgroundColor = .white } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
修改 AppDelegate 类
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { let drawerController = DrawerController(centerViewController: UINavigationController(rootViewController: ViewController()), leftDrawerViewController: UINavigationController(rootViewController: LeftViewController())) // 侧滑打开宽度 drawerController.maximumLeftDrawerWidth = 250 // 打开侧滑手势 drawerController.openDrawerGestureModeMask = .all // 关闭侧滑手势 drawerController.closeDrawerGestureModeMask = .all self.window?.rootViewController = drawerController return true }
Navigation上添加按钮
icon可以在这里下载:https://www.jb51.net/softs/578475.html
修改 ViewController
import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() self.title = "DrawerDemo" self.view.backgroundColor = .white // 给导航条添加一个按钮 self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "baseline-menu-48px"), style: .plain, target: self, action: #selector(ViewController.openLeftMenu)) self.navigationController?.navigationBar.barStyle = .default // menu icon默认是蓝 ,下面将其改成黑 的 self.navigationController?.navigationBar.tintColor = .black } @objc func openLeftMenu() { // 打开drawerController self.navigationController?.evo_drawerController?.toggleLeftDrawerSide(animated: true, completion: nil) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
您可能感兴趣的文章
- 01-11swift中defer几个简单的使用场景详解
- 01-11Swift开发应用中如何更方便地使用颜 详解
- 01-11swift4.0实现视频播放、屏幕旋转、倍速播放、手势调节及锁屏面
- 01-11Swift3迁移至Swift4可能遇到的问题小结
- 01-11Swift中defer的正确使用方法
- 01-11在Swift中如何使用正则表达式详解
- 01-11Swift学习教程之SQLite的基础使用
- 01-11Swift 4.2使用self做为变量名浅析
- 01-11Swift4.1转场动画实现侧滑抽屉效果
- 01-11Swift如何使用类型擦除及自定义详解