LoginSignup
17
19

More than 5 years have passed since last update.

スライドメニュー(SWRevealViewController)[再々追記あり]

Last updated at Posted at 2015-12-25

スライドメニュー欲しくて
iOS-Slide-Menuっての試したけどObjective-cだとうまくいったんだけど、Swiftだとうまくいかないから
SWRevealViewControllerっていうのを試して見たらうまくいったので使い方まとめ。

基本的にはチュートリアル通りにやればいいかなと
“SWRevealViewControllerSetSegue”ってなってる部分はSWRevealViewControllerSegueSetControllerです。
podsでuse_frameworks!を書いてインストールする場合は、以前書いた通りimport SWRevealViewControllerで見つかるようになります。

大雑把にですが以下説明

最終的なストーリーボードはこんな感じです。
スクリーンショット_2015-12-26_0_27_25.png

①ViewControllerを追加
 追加したストーリーボードスクリーンショット 2015-12-26 0.34.25.png
 ClassにSWRevealViewControllerを設定

②NavigationControllerとViewControllerのセットを作成
 NavigationControllerにボタンを表示するのでNavigationControllerは必要です。
 それに紐づく.swiftも作成。
 各ビューにメニューボタンも自分で置く

③メニュー用のViewControllerを作成
 今回のサンプルは固定メニューのためにstatic Cellsを設定したいのでTableViewControllerを使ってます。
スクリーンショット 2015-12-26 0.43.43.png

④SWRevealViewControllerとメニューのひも付け
 スクリーンショット 2015-12-26 0.45.12.png
 右クリックでピーって引っ張ってCustomを選択して
スクリーンショット 2015-12-26 0.47.22.png
 Identifierにsw_rear classにSWRevealViewControllerSegueSetController
 を設定。これがサイドメニューの設定

⑤各画面をつなぐ
 初期画面はSWRevealViewControllerからreveal view controller set controllerを選択して
スクリーンショット 2015-12-26 0.52.06.png
 追加でIdentifierにsw_frontを設定
 他の2画面と初期画面をTableViewのCellからreveal view controller push controllerで各々つなぐ

⑥ViewController.swiftの編集
 メニューボタンをmenuButtonで設定して
swift:sample
if self.revealViewController() != nil {
menuButton.target = self.revealViewController()
menuButton.action = "revealToggle:"
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}

スクリーンショット 2015-12-26 0.55.39.png

ッて感じで動く!

サンプルGitに置いときます。

追記

メイン画面タップで閉じるようにする場合は
objc:ViewController.swift
let revealController = self.revealViewController()
revealController.tapGestureRecognizer()

を足せばいいみたい。

さらなる追記

コードで遷移する場合は
objc:sample
let storyboard:UIStoryboard = UIStoryboard.init(name: "CardListViewController", bundle: NSBundle.mainBundle())
let cardListViewcontroller:CardListViewController = storyboard.instantiateInitialViewController()! as! CardListViewController
cardListViewcontroller.typeName = types[indexPath.row]
let navigationController = UINavigationController(rootViewController: cardListViewcontroller)
let revealController = self.revealViewController()
revealController.pushFrontViewController(navigationController, animated: true)

ッて感じで
ViewController作成

NavigationController作成

revealViewControllerにpushとやるみたい

17
19
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
17
19