[swift4] スライドメニューを実現できる「SlideMenuControllerSwift」の実装方法
スライドメニューを実現できる「SlideMenuControllerSwift」の実装方法は以下の通り
https://github.com/dekatotoro/SlideMenuControllerSwift
目次
Podでインストール
Podファイルに以下のコードを追加
pod 'SlideMenuControllerSwift', :git => 'https://github.com/dekatotoro/SlideMenuControllerSwift', :branch => 'swift4'
追加したら
pod install --repo-update
でインストール。
概要
SlideMenuControllerという土台にMainとLeft、Rightという最大3枚のViewControllerを追加することができる。
最初はMainが表示されるが、Leftを開くという命令が発行されるとLeftと定義しているViewControllerが左に指定サイズだけ表示される。
Leftが表示されている間でもMainのスレッドは停止せずに実行しつづける。
コード実装
MainViewController.swift
import SlideMenuControllerSwift class MainViewController: UIViewController { ... @objc private func didTapLeftMenuIcon() { self.slideMenuController()?.openLeft() } @objc private func didTapRightMenuIcon() { self.slideMenuController()?.openRight() } ... }
LeftViewContoller.swift
import UIKit class LeftMenuViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } }
StoryBoard上のIDに「Left」と設定
RightViewContoller.swift
import UIKit class RightViewContoller: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } }
StoryBoard上のIDに「Right」と設定
AppDelegate.swift
import SlideMenuControllerSwift @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. let mainVC = storyboard.instantiateViewController(withIdentifier: "Main") let rightVC = storyboard.instantiateViewController(withIdentifier: "Right") as! RightViewController let leftVC = storyboard.instantiateViewController(withIdentifier: "Left") as! LeftViewController let viewController = SlideMenuController(mainViewController: mainVC, leftMenuViewController: leftVC, rightMenuViewController: rightVC) self.window?.rootViewController = viewController self.window?.makeKeyAndVisible() return true }
NavigationBarを追加したい場合は、
import SlideMenuControllerSwift @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. let mainVC = storyboard.instantiateViewController(withIdentifier: "Main") let rightVC = storyboard.instantiateViewController(withIdentifier: "Right") as! RightViewController let leftVC = storyboard.instantiateViewController(withIdentifier: "Left") as! LeftViewController let navigationController = UINavigationController(rootViewController: mainVC!) let viewController = SlideMenuController(mainViewController: navigationController, leftMenuViewController: leftVC, rightMenuViewController: rightVC) self.window?.rootViewController = viewController self.window?.makeKeyAndVisible() return true }
補足
LeftやRightのViewControllerでMainのViewContollerの関数を実行するには、通常のSwiftと同じくProtocolを設定してDelegateすれば良い。
RightViewContoller.swift
import UIKit protocol RightViewControllerDelegate: class { func didselectMode(mode:String) } class RightViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { @IBOutlet weak var tableView: UITableView! weak var delegate: RightViewControllerDelegate? //要素 var posts:[string] = ["Google","Apple","Facebook","Amazon"] //MARK: didload override func viewDidLoad() { super.viewDidLoad() tableView.dataSource = self tableView.delegate = self } //MARK: TableView Method func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return posts.count } func numberOfSections(in tableView: UITableView) -> Int { // sectionの数を決める return 1 } //MARK: cellSetting func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) let label = cell.contentView.viewWithTag(1) as! UILabel label.text = posts[indexPath.row] return cell } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { self.delegate?.didselectMode(mode: posts[indexPath.row]!) } }
MainViewContoller.swift
extension MainViewController: RightViewControllerDelegate { func didselectMode(mode: String) { print("didselectMode is \(mode)") self.closeRight() //右を閉じる //その他処理 }
AppDelegate.swift
import SlideMenuControllerSwift @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. let mainVC = storyboard.instantiateViewController(withIdentifier: "Main") let rightVC = storyboard.instantiateViewController(withIdentifier: "Right") as! RightViewController let leftVC = storyboard.instantiateViewController(withIdentifier: "Left") as! LeftViewController rightVC.delegate = mainVC as! RightViewControllerDelegate //重要 let viewController = SlideMenuController(mainViewController: mainVC, leftMenuViewController: leftVC, rightMenuViewController: rightVC) self.window?.rootViewController = viewController self.window?.makeKeyAndVisible() return true }
NavigationBarを追加したい場合は、
これでRightViewContollerのTableViewのセルを選択した際に、指定StringをMainに渡すことができる。