例えば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で画像を分けるよりも画像リソースが減るので経済的。