本文介紹了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上添加按鈕
修改 ViewController
import UIKitclass 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. }}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。
新聞熱點
疑難解答