-1

I have tried setSelectedItem in viewDidLoad, also tried selectedIndex etc.. whatever I found on net.. But I couldn't find any answer. Each item when I am launching the app it shows tab bar in default state not in selected state. I am also getting error

Directly tab bar cannot be modified
ilse2005
  • 11,189
  • 5
  • 51
  • 75

2 Answers2

0

You want to do something like this:

UIImage *item2Image = [[UIImage imageNamed:@"simulatorTabBarIconUnselected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        UIImage *item2ImageSelected = [[UIImage imageNamed:@"simulatorTabBarIconSelected"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        UITabBarItem *tabBarItem2 = [[UITabBarItem alloc] initWithTitle:@"Simulator" image:item2Image selectedImage:item2ImageSelected];

Setting the selected image to UIImageRenderingModeAlwaysTemplate will tint it as your tint color. For the unselected image, apple defaults this to gray no matter what, so the only way around it is to set it to UIImageRenderingModeAlwaysOriginal and have the image asset be the color you want it to be. In my case I just made an image asset of the original image but with the alpha at 50% so it looked faded out but still has the same tint.

arc4randall
  • 3,275
  • 3
  • 27
  • 40
  • I had tried this UITabBarItem *tabItem = [[UITabBarItem alloc] initWithTitle:@"Discuss" image:[[UIImage imageNamed:@"u_discuss"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"discuss_active"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]]; but still no effect.. help!! – Prathma Rastogi Feb 29 '16 at 18:54
  • set the navigation controllers tabBarItem property to the code above. Your tab Bar should contain NavigationControllers, which each contain view controllers – arc4randall Feb 29 '16 at 18:59
  • I am adding this in the view controller itself. Tell me how to add this property – Prathma Rastogi Mar 01 '16 at 06:28
  • I had made the function for UITabBarItem in view controller itself - (UITabBarItem*)tabBarItem { UITabBarItem *tabItem = [[UITabBarItem alloc] initWithTitle:@"Discuss" image:[[UIImage imageNamed:@"u_discuss"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"discuss_active"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]]; return tabItem; } – Prathma Rastogi Mar 01 '16 at 06:30
  • I only know how to do this the way I described. If you have a tab bar controller, you should make its view controllers all navigation controllers. These navigation controllers will contain each of the view controllers you want to display, and the navigation controller will have the tabBarItem property. The view controller will NOT have the tabBarItem property – arc4randall Mar 01 '16 at 06:30
0
 let accountBoard = UIStoryboard(name: "Account", bundle: nil)
    let accountNav = accountBoard.instantiateInitialViewController() as! CommonNavigationController
    accountNav.tabBarItem = UITabBarItem(title: "Account", image: UIImage(named: "icon - account")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), selectedImage: UIImage(named: "icon - account - white")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal))
    accountNav.tabBarItem.setTitleTextAttributes([NSFontAttributeName: STYLES.avenirNextDemiBold11!,NSForegroundColorAttributeName: UIColor.whiteColor()], forState: .Normal)
    accountNav.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.redColor()], forState:UIControlState.Selected)

You can programmatically change selected-unselected items' color attributes.

Mirsat Murutoglu
  • 202
  • 2
  • 11