LoginSignup
31
31

More than 5 years have passed since last update.

TabBarControllerを使う Swift版

Last updated at Posted at 2015-03-14

このメモはswiftもUIKitについてもほぼ知識ゼロな人が勉強のメモとして書いています。色々間違っている可能性があるので、ご注意下さい

プロジェクト作成時の設定

  1. プロジェクト作成時にTabbed Applicaitonを選択するだけ

新しいタブの追加方法

  1. Storyboardを開く
  2. ViewControllerを追加する
  3. TabBarControlelrをStoryboard上で2.で追加したViewにControl+Dragする
  4. RelationshipSegue - view controllersを選ぶ

iOS Quick Tip: Creating a UITabBar Application with Storyboards

タブのアイコンの変更方法

  1. Storyboard上で変更したいViewのタブ部分を選択
  2. Attributes InspectorでSystem Itemを変更 Customにすれば好きなアイコンを表示出来る?

カスタムTabController導入手順

1 UITabBarControllerを継承したコントローラを定義
2 UITabBarControllerDelegate protocolを定義したクラスのインスタンスをdelegateメンバ変数に設定する

import UIKit

class MyTabBarController: UITabBarController, UITabBarControllerDelegate  {
    override func viewDidLoad() {
        println("viewDidLoad")
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.delegate = self
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    //UITabBarControllerDelegateプロトコルを実装する
    func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {
        println("shouldSelectViewController")
        return true
    }
    //UITabBarControllerDelegateプロトコルを実装する    
    func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
        println("didSelectViewController")
    }

}

3 Interface Builder上でTab Bar Controllerを選択し、定義したカスタムTabControllerクラスを下記のように指定する

スクリーンショット 2015-03-14 22.40.59.png

TabBar上にバッジを表示する

  • Content view controlelrが保持するtabBarItemプロパティのbadgeValueに値を設定してあげればいい
  • 以下は選択されたタブにバッジを付ける実用性のないサンプル
    func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
        viewController.tabBarItem.badgeValue = "1"
    }

参考

UITabBarController Class Reference

View Controller Catalog for iOS

31
31
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
31
31