swift4 使用DrawerController實現側滑菜單功能的示例代碼
更新時間:2018年06月30日 14:59:38 作者:朋也
這篇文章主要介紹了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可以在這里下載:http://www.fzitv.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.
}
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

