「Ios/swift/外部ライブラリ/DrawerController」の版間の差分
提供: 初心者エンジニアの簡易メモ
(同じ利用者による、間の3版が非表示) | |||
行12: | 行12: | ||
==ナビゲーションバー付きドロワーメニューを作成== | ==ナビゲーションバー付きドロワーメニューを作成== | ||
− | #ViewControllerのStoryboardIDに" | + | #ViewControllerのStoryboardIDに"CenterNavigationControllerId"を追加 |
#以下の通りswiftファイルを編集する | #以下の通りswiftファイルを編集する | ||
− | #storyboardに新しくViewControllerを追加し、クラス名をDrawerViewControllerとし、StoryboardIDを" | + | #storyboardに新しくViewControllerを追加し、クラス名をDrawerViewControllerとし、StoryboardIDを"DrawerViewControllerId"にする |
-AppDelegate.swift | -AppDelegate.swift | ||
行22: | 行22: | ||
class AppDelegate: UIResponder, UIApplicationDelegate { | class AppDelegate: UIResponder, UIApplicationDelegate { | ||
var window: UIWindow? | var window: UIWindow? | ||
+ | var navigationController: UINavigationController! | ||
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { | func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { | ||
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle()) | let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle()) | ||
− | let centerViewController: ViewController! = storyboard.instantiateViewControllerWithIdentifier(" | + | let centerViewController: ViewController! = storyboard.instantiateViewControllerWithIdentifier("CenterNavigationControllerId") as! ViewController |
− | + | navigationController = UINavigationController(rootViewController: centerViewController) | |
− | let drawerViewController: DrawerViewController! = storyboard.instantiateViewControllerWithIdentifier(" | + | let drawerViewController: DrawerViewController! = storyboard.instantiateViewControllerWithIdentifier("DrawerViewControllerId") as! DrawerViewController |
let drawerController: DrawerController! = DrawerController(centerViewController: navigationController, leftDrawerViewController: drawerViewController) | let drawerController: DrawerController! = DrawerController(centerViewController: navigationController, leftDrawerViewController: drawerViewController) | ||
drawerController.showsShadows = true // 影付き | drawerController.showsShadows = true // 影付き | ||
行58: | 行59: | ||
} | } | ||
} | } | ||
+ | |||
+ | #左メニューのViewのDrawerViewControllerのstoryboardに2つButton設置 | ||
+ | #1つ目のボタンをDrawerViewControllerのコードにControl+ドラッグで引っ張り"firstButton"で紐付ける | ||
+ | #2つ目のボタンをDrawerViewControllerのコードにControl+ドラッグで引っ張り"secondButton"で紐付ける | ||
+ | |||
-DrawerViewController.swift | -DrawerViewController.swift | ||
import UIKit | import UIKit | ||
class DrawerViewController: UIViewController { | class DrawerViewController: UIViewController { | ||
+ | @IBOutlet weak var firstButton: UIButton! | ||
+ | @IBOutlet weak var secondButton: UIButton! | ||
override func viewDidLoad() { | override func viewDidLoad() { | ||
super.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() { | override func didReceiveMemoryWarning() { | ||
行71: | 行94: | ||
==ボタンはこちらを参考== | ==ボタンはこちらを参考== | ||
[[ios/swift/ButtonSystem]] [ショートカット] | [[ios/swift/ButtonSystem]] [ショートカット] | ||
+ | |||
+ | ==storyboardのナビゲーションViewを取り込む方法== | ||
+ | NavigationControllerのStoryboradIDに"CenterNavigationControllerId"を追加し、navigationControllerを以下に差し替える。 | ||
+ | let navigationController: UINavigationController! = storyboard.instantiateViewControllerWithIdentifier("CenterNavigationControllerId") as! UINavigationController | ||
==参考== | ==参考== | ||
*http://engineer-terminal.com/2016/01/09/%E3%82%B9%E3%83%9E%E3%83%9B%E3%82%A2%E3%83%97%E3%83%AA%E3%81%A7%E3%81%AB%E3%81%AF%E6%AC%A0%E3%81%8B%E3%81%9B%E3%81%AA%E3%81%84%E3%81%82%E3%82%8B%E3%82%A2%E3%83%AC%E3%82%92%E4%BD%9C%E3%82%8B-swift/ | *http://engineer-terminal.com/2016/01/09/%E3%82%B9%E3%83%9E%E3%83%9B%E3%82%A2%E3%83%97%E3%83%AA%E3%81%A7%E3%81%AB%E3%81%AF%E6%AC%A0%E3%81%8B%E3%81%9B%E3%81%AA%E3%81%84%E3%81%82%E3%82%8B%E3%82%A2%E3%83%AC%E3%82%92%E4%BD%9C%E3%82%8B-swift/ | ||
*https://teratail.com/questions/25333 | *https://teratail.com/questions/25333 |
2016年6月27日 (月) 05:22時点における最新版
公式
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