Ios/swift/外部ライブラリ/DrawerController
提供: 初心者エンジニアの簡易メモ
公式
https://github.com/sascha/DrawerController/
インストール
$ vi Podfile platform :ios, '9.0' target 'HelloworldDrawer' do use_frameworks! pod "DrawerController", '~> 1.0' end $ pod install
ナビゲーションバー付きドロワーメニューを作成
- ViewControllerのStoryboardIDに"CenterNavigationControllerId"を追加
- 以下の通りswiftファイルを編集する
- storyboardに新しくViewControllerを追加し、クラス名をDrawerViewControllerとし、StoryboardIDを"DrawerViewControllerId"にする
-AppDelegate.swift
import UIKit import DrawerController @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? var navigationController: UINavigationController! func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle()) let centerViewController: ViewController! = storyboard.instantiateViewControllerWithIdentifier("CenterNavigationControllerId") as! ViewController navigationController = UINavigationController(rootViewController: centerViewController) let drawerViewController: DrawerViewController! = storyboard.instantiateViewControllerWithIdentifier("DrawerViewControllerId") as! DrawerViewController let drawerController: DrawerController! = DrawerController(centerViewController: navigationController, leftDrawerViewController: drawerViewController) drawerController.showsShadows = true // 影付き drawerController.restorationIdentifier = "Drawer" drawerController.maximumLeftDrawerWidth = 240.0 // 幅240 drawerController.openDrawerGestureModeMask = .All // タッチ操作を全て受け付ける drawerController.closeDrawerGestureModeMask = .All self.window = UIWindow(frame: UIScreen.mainScreen().bounds) self.window?.rootViewController = drawerController self.window?.makeKeyAndVisible() return true } // 略 }
-ViewController.swift
import UIKit import DrawerController class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = UIColor.blueColor() let btn = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Bookmarks, target: self, action:#selector(ViewController.drawerOnClickButton(_:))) self.navigationItem.setLeftBarButtonItems([btn], animated: true) } // 左メニューを開く func drawerOnClickButton(sender: AnyObject?) { self.evo_drawerController?.toggleDrawerSide(.Left, animated: true, completion: nil) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }
- 左メニューのViewのDrawerViewControllerのstoryboardに2つButton設置
- 1つ目のボタンをDrawerViewControllerのコードにControl+ドラッグで引っ張り"firstButton"で紐付ける
- 2つ目のボタンをDrawerViewControllerのコードにControl+ドラッグで引っ張り"secondButton"で紐付ける
-DrawerViewController.swift
import UIKit class DrawerViewController: UIViewController { @IBOutlet weak var firstButton: UIButton! @IBOutlet weak var secondButton: UIButton! override func viewDidLoad() { super.viewDidLoad() firstButton.addTarget(self, action: #selector(DrawerViewController.firstOnClickMyButton(_:)), forControlEvents: .TouchUpInside) secondButton.addTarget(self, action: #selector(DrawerViewController.secondOnClickMyButton(_:)), forControlEvents: .TouchUpInside) } func firstOnClickMyButton(sender: UIButton) { changeViewController("FirstViewControllerId") } func secondOnClickMyButton(sender: UIButton) { changeViewController("SecondViewControllerId") } private func changeViewController(storyboardId: String) { let appDelegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle()) let centerViewController: UIViewController! = storyboard.instantiateViewControllerWithIdentifier(storyboardId) appDelegate.navigationController?.pushViewController(centerViewController, animated: false) self.evo_drawerController?.toggleDrawerSide(.Left, animated: true, completion: nil) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }
ボタンはこちらを参考
ios/swift/ButtonSystem [ショートカット]
storyboardのナビゲーションViewを取り込む方法
NavigationControllerのStoryboradIDに"CenterNavigationControllerId"を追加し、navigationControllerを以下に差し替える。
let navigationController: UINavigationController! = storyboard.instantiateViewControllerWithIdentifier("CenterNavigationControllerId") as! UINavigationController