You can do as below to assign image to tab bar.
let myTab = self.tabBarController!.viewControllers?[0].tabBarItem as UITabBarItem! //get the desire tab by passing index as 0,1,2,3... Currently i am pointing to first tab.
myTab.image = UIImage(named: "image1")//This image will appear when tab is not selected
myTab.selectedImage = UIImage(named: "image2")//This image will appear when tab is selected
Now to check wether you are clicking same tab or not, implement the delegate method,
func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
if viewController.tabBarItem.tag == 0 { // assuming this is your desired so called record tab
if !self.isRecordSelected {// create a property isRecordSelected of Bool type
tabBarItem.selectedImage = UIImage(named: "new record image")//This image will appear when tab is recording starts.
self.isRecordSelected = true
} else {
tabBarItem.selectedImage = UIImage(named: " revert image")//This image will appear when tab is recording stops.
self.isRecordSelected = false
}
}
}
Give tag number to each tab, so that it will be easy for identification. Set the UITabBarDelegate delegate in your class.