5
5

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.

SwiftでUITabBarControllerの選択色をタブごとに変える

Last updated at Posted at 2016-03-19

例えばStoryboard上で

MyTabBarController
   -> UINavigationController -> MyNavigationControllerA
   -> UINavigationController -> MyNavigationControllerB
   -> UINavigationController -> MyNavigationControllerC

という構成になっている場合(MyTabBarControllerのdelegateはself)

MyTabBarController.swift
func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {
    guard let tabContent = viewController as? UINavigationController else {
        return true
    }
    let navigationContent = tabContent.viewControllers[0]
    if nil != navigationContent as? MyNavigationControllerB {
        // Bの時だけ選択色を赤に
        tabBar.tintColor = UIColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0)
    } else {
        // それ以外は緑に
        tabBar.tintColor = UIColor(red: 0.0, green: 1.0, blue: 0.0, alpha: 1.0)
    }
    return true
}

とすれば動的に選択色が変わる。
selectedImageとimageで画像を分けるよりも画像リソースが減るので経済的。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?