The icons show fine in ios 6 but not in ios 7. I\'m setting the selected state in the viewController viewDidLoad method. When the user selects a tab bar item the image disap
Swift version of showing selected and unselected images and title with UIAppearance API In your Appdelegate.m copy following code if you have tab base app.following code assume you have 4 tab bar.
let tabBarController: UITabBarController = (self.window!.rootViewController as! UITabBarController)
let tabBar:UITabBar = tabBarController.tabBar
let tabBarItem1:UITabBarItem = tabBar.items![0]
let tabBarItem2:UITabBarItem = tabBar.items![1]
let tabBarItem3:UITabBarItem = tabBar.items![2]
let tabBarItem4:UITabBarItem = tabBar.items![3]
tabBarItem1.title = "Home";
tabBarItem2.title = "Maps";
tabBarItem3.title = "My Plan";
tabBarItem4.title = "Settings";
tabBarItem1.selectedImage = UIImage(named: "home_selected.png")!
tabBarItem2.selectedImage = UIImage(named: "maps_selected.png")!
tabBarItem3.selectedImage = UIImage(named: "myplan_selected.png")!
tabBarItem4.selectedImage = UIImage(named: "settings_selected.png")!
tabBarItem1.image = UIImage(named: "home.png")!
tabBarItem2.image = UIImage(named: "maps.png")!
tabBarItem3.image = UIImage(named: "myplan.png")!
tabBarItem4.image = UIImage(named: "settings.png")!
let tabBarBackground: UIImage = UIImage(named: "tabbar.png")!
UITabBar.appearance().backgroundImage = tabBarBackground
UITabBar.appearance().selectionIndicatorImage = UIImage(named: "tabbar_selected.png")!
UITabBarItem.appearance().setTitleTextAttributes([
NSForegroundColorAttributeName : UIColor.whiteColor()
]
, forState: .Normal)
let titleHighlightedColor: UIColor = UIColor(red: 153 / 255.0, green: 192 / 255.0, blue: 48 / 255.0, alpha: 1.0)
UITabBarItem.appearance().setTitleTextAttributes([
NSForegroundColorAttributeName : titleHighlightedColor
]
, forState: .Highlighted)
You need to use tabBarItem initWithTitle:image:selectedImage
[[UITabBarItem alloc] initWithTitle:@"title" image:image selectedImage:imageSel];
in conjunction with changing the UIImage rendering mode:
imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal
or (to apply parent views template tint mask, this option is default for Tab bar Items unless you opt out with the above rendering mode)
imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate
here is a code sample for one tab bar item :-
UIImage *musicImage = [UIImage imageNamed:@"music.png"];
UIImage *musicImageSel = [UIImage imageNamed:@"musicSel.png"];
musicImage = [musicImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
musicImageSel = [musicImageSel imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.musicViewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Music" image:musicImage selectedImage:musicImageSel];
Hope this helps