6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

MMDrawerControllerをSwiftで使う

6
Last updated at Posted at 2016-06-06

初めに

MMDraweControllerでドロワーをする方法があまりなかったので書きます
こちら参考にされていただきました
使用環境は
XCode 7.3
iOS 9.3
です

CocoaPodsでダウンロードする

作成したプロジェクトでPodfileを作成する

platform :ios, "9.3"
pod 'MMDrawerController', '~> 0.5.7'

ライブラリをインストールする
Podfileを作成したディレクトリで以下のコマンドを実行する

pod install

インストールに成功すると hoge.xcworkspace ができていると思うのでこれを開きます

プロジェクトにMMDrawerControllerを組み込む

Objectiv-Cのダミーファイルを作成します
名前はなんでもいいのでObjectiv-Cファイルを新規作成すると
Would you like to configure an Objective-C bridging header? となるので Create Bridging Header をクリックします
ここで作成された hoge-Bridging-Header.h を開き以下のようにします

hoge-Bridging-Header.h
# import "MMDrawerController.h"

storyboardにview controllerを追加する

Main.storyboardを開き、もともとあるViewControllerのstoryboard IDをCenterViewにします
ドロワー用のView Controllerを追加します 
この時に左右どちらかのドロワーだけでいいのなら1つ
左右両方とも欲しいのなら2つ追加しておきます
追加したView Controller の Storyboard ID を LeftSideViewやRightSeideViewなどの分かりやすい名前にしておきます

View Controller用のクラスを用意する

ドロワー用のクラスを用意するため以下のようなクラスを作成します

HogeViewController
import UIKit

class HogeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

追加したドロワークラスをstoryboardで追加したView controllerのクラスに紐付けます

AppDelegateの記述

AppDelegateの内容を以下のように変更します

AppDelegate
var centerContainer: MMDrawerController?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        
        let centerViewController = mainStoryboard.instantiateViewControllerWithIdentifier("CenterView") as! ViewController
        
        let leftViewController = mainStoryboard.instantiateViewControllerWithIdentifier("LeftSideView") as! HogeViewController
 //		let leftViewController = mainStoryboard.instantiateViewControllerWithIdentifier("RightSideView") as! HogeViewController
        
        let leftSideNav = UINavigationController(rootViewController: leftViewController)
        let centerNav = UINavigationController(rootViewController: centerViewController)
 //		let rightNav = UINavigationController(rootViewController: rightViewController)
        
        centerContainer = MMDrawerController(centerViewController: centerNav, leftDrawerViewController: leftSideNav,rightDrawerViewController:nil)
        
        //オープン方法のモード指定
        centerContainer!.openDrawerGestureModeMask = MMOpenDrawerGestureMode.BezelPanningCenterView
        
        //クローズ方法のモード指定
        centerContainer!.closeDrawerGestureModeMask = MMCloseDrawerGestureMode.All
        
        window!.rootViewController = centerContainer
        window!.makeKeyAndVisible()
        return true
    }

オープン方法の一覧は以下の表を参照してください

モード 内容
BezelPanningCenterView 端からスワイプで開く
PanningCenterView センターをスワイプして開く
PanningNavigationBar ナビゲーションバーをスワイプして開く
All 上記のすべての方法で開く

クローズ方法は以下の表の通りです

モード 内容
BezelPanningCenterView 端からスワイプで閉じる
PanningCenterView センターをスワイプして閉じる
PanningDrawerView ドロワーをスワイプして閉じる
PanningNavigationBar ナビゲーションバーをスワイプして閉じる
All 上記のすべての方法で閉じる

追記

CenterView内のボタンでドロワーを開く

Center View内に配置されたボタンからViewControllerクラスにIBActionを設定します
IBActionの中を以下のようにするとドロワーがボタンでもできます

ViewController.swift
@IBAction func hamburgerButtonTapped(sender: AnyObject) {
        let appDelegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        appDelegate.centerContainer!.toggleDrawerSide(MMDrawerSide.Left,animated: true, completion:nil)
}
6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?