Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing selectedImage on UITabBarItem in Swift

I've been trying to change the selected image on a UITabBar. I've followed procedures listed on other Stackoverflow questions, but nothing seems to work.

I have tried setting the image through the User Defined Runtime Attributes section, as well as tried adding the following to AppDelegate.swift:

var tabBarController = self.window!.rootViewController as UITabBarController
let tabItems = tabBarController.tabBar.items as [UITabBarItem]

var selectedImage0 = UIImage(named:"NewsfeedTabSelected")
selectedImage0?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
tabItems[0].selectedImage = selectedImage0

This doesn't yield any results. I did a println(tabItems[0].title) and that output the correct title, so I know that the reference to the TabBarItem is working.

Any thoughts?

like image 800
matcartmill Avatar asked Jan 11 '15 19:01

matcartmill


People also ask

How do I change the color of a tab bar in Swift?

This elegant solution works great on SWIFT 3.0, SWIFT 4.2 and SWIFT 5.1: On the Storyboard: Select your Tab Bar. Set a Runtime Attibute called tintColor for the desired color of the Selected Icon on the tab bar.

How do I create a custom tab bar in Swift?

To add a Tab Bar Controller to the Storyboard, select the 4 placeholders that we just created and the View Controller. Then, go to Editor, select Embed in and Tab Bar Controller. This will envelop all those scenes in a single Tab Bar Controller. Put the new Tab Bar Controller on top of the other controllers.


2 Answers

I solved using something like this on the AppDelegate.

var tabBarController = self.window!.rootViewController as UITabBarController
var tabBar = tabBarController.tabBar as UITabBar

var tabBarItem1 = tabBar.items![0] as UITabBarItem
var tabBarItem2 = tabBar.items![1] as UITabBarItem
var tabBarItem3 = tabBar.items![2] as UITabBarItem

tabBarItem1.selectedImage = UIImage(named: "FirstSelectedImage")
tabBarItem2.selectedImage = UIImage(named: "SecondSelectedImage")
tabBarItem3.selectedImage = UIImage(named: "ThirdSelectedImage")
like image 75
Olav Gausaker Avatar answered Sep 20 '22 10:09

Olav Gausaker


The previous answer does not fully work. I have to set the new UIImage's UIImageRenderingMode to AlwaysOriginal, this solves my situation.

code bellow:

import UIKit

class MainTab: UITabBarController {

override func viewDidLoad() {

    var tabBar = self.tabBar

    var homeSelectImage: UIImage! = UIImage(named: "firstPageSelected")?.imageWithRenderingMode(.AlwaysOriginal)
    var qaSelectImage: UIImage! = UIImage(named: "Q&ASelected")?.imageWithRenderingMode(.AlwaysOriginal)
    var mySelectImage: UIImage! = UIImage(named: "myBagSelected")?.imageWithRenderingMode(.AlwaysOriginal)

    (tabBar.items![0] as! UITabBarItem ).selectedImage = homeSelectImage
    (tabBar.items![1] as! UITabBarItem ).selectedImage = qaSelectImage
    (tabBar.items![2] as! UITabBarItem ).selectedImage = mySelectImage

    tabBar.tintColor = UIColor.greenColor()

}
}

Hope that work for you

like image 21
Jess Avatar answered Sep 19 '22 10:09

Jess