読者です 読者をやめる 読者になる 読者になる

【swift】UITabBarControllerにスライドメニューを追加する方法

はじめに

スライドメニューで検索すると色々出てきたのですが、今回は
SlideMenuControllerSwiftというライブラリを使わせていただきました。

紹介されている使い方をそのままでした。
Podfileに以下を追加して、podinstallするとインストール完了です

Podfile

use_frameworks!
pod 'SlideMenuControllerSwift'

実装

AppDelegate.swift

import UIKit
import SlideMenuControllerSwift

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        let tabbarController: TabBarController = TabBarController()
        let menuController = MenuViewController()
        
        let slideMenuController = SlideMenuController(mainViewController: tabbarController, leftMenuViewController: menuController)
        
        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        self.window?.rootViewController = slideMenuController
        self.window?.makeKeyAndVisible()
        
        return true
    }

    ・・・
}

TabBarController.swift

class TabBarController: UITabBarController {

    var navController: UINavigationController!
    var navController2: UINavigationController!
    var navController3: UINavigationController!
    var navController4: UINavigationController!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        let vc = ViewController()
        navController = UINavigationController(rootViewController: vc)
        
        let vc2 = ViewController()
        navController2 = UINavigationController(rootViewController: vc2)
        
        let vc3 = ViewController()
        navController3 = UINavigationController(rootViewController: vc3)
        
        let vc4 = ViewController()
        navController4 = UINavigationController(rootViewController: vc4)
        
        // タブ画像設定
        navController.tabBarItem.image = UIImage(named: "icon.png")
        navController2.tabBarItem.image = UIImage(named: "icon.png")
        navController3.tabBarItem.image = UIImage(named: "icon.png")
        navController4.tabBarItem.image = UIImage(named: "icon.png")
        
        // タブタイトル設定
        navController.tabBarItem.title = "タブ1"
        navController2.tabBarItem.title = "タブ2"
        navController3.tabBarItem.title = "タブ3"
        navController4.tabBarItem.title = "タブ4"
        
        let tabs = NSArray(objects: navController, navController2, navController3, navController4)
        self.setViewControllers(tabs as? [UIViewController], animated: true)
    }
}

ViewController.swift

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.view.backgroundColor = UIColor.whiteColor()
        
        self.navigationController?.navigationBar.tintColor = UIColor.whiteColor()
        self.navigationController?.navigationBar.barTintColor = UIColor.blackColor()
        self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
        
        let iconHome = UIBarButtonItem(image: UIImage(named: "menu.png"), style: .Plain, target: self, action: "onMenu:")
        self.navigationItem.leftBarButtonItem = iconHome
    }

    func onMenu(sender: UIButton) {
        self.slideMenuController()?.openLeft()
    }
}

便利でかっこいいです。
以上です