Changing UITabBar item image on selected and unselected state for Three Tabs but getting error

I have implemented three tab bar view controllers for my app. Each tab has collection views, And I have implemented following code in my ViewController to change tab bar image icon from stroke to fill on respective tab bar selection. I am using System Icons (SF Symbols).

My Code:

Code Block
self.tabBarController?.tabBar.items?[0] = UITabBarItem(title: "Myanmar", image: UIImage(systemName: "m.square.fill"), selectedImage: UIImage(systemName: "m.square"))
self.tabBarController?.tabBar.items?[1] = UITabBarItem(title: "Singapore", image: UIImage(systemName: "s.square"), selectedImage: UIImage(systemName: "s.square.fill"))
self.tabBarController?.tabBar.items?[2] = UITabBarItem(title: "Hyderabad", image: UIImage(systemName: "h.square"), selectedImage: UIImage(systemName: "h.square.fill"))


Error that I am getting at AppDelegate is: "Directly modifying a tab bar managed by a tab bar controller is not allowed."

How to solve this?
Answered by OOPer in 671623022
You can modify the properties of each UITabBarItem:
Code Block
if let tabBarItem1 = self.tabBarController?.tabBar.items?[0] {
tabBarItem1.title = "Myanmar"
tabBarItem1.image = UIImage(systemName: "m.square.fill")
tabBarItem1.selectedImage = UIImage(systemName: "m.square")
}
if let tabBarItem2 = self.tabBarController?.tabBar.items?[1] {
tabBarItem2.title = "Singapore"
tabBarItem2.image = UIImage(systemName: "s.square.fill")
tabBarItem2.selectedImage = UIImage(systemName: "s.square")
}
if let tabBarItem3 = self.tabBarController?.tabBar.items?[2] {
tabBarItem3.title = "Hyderabad"
tabBarItem3.image = UIImage(systemName: "h.square.fill")
tabBarItem3.selectedImage = UIImage(systemName: "h.square")
}


Where do you do this ?

If need to change a tabBar item, you should not in the UITabBarController, but in each UIViewController, in its viewDidload and viewWillDisappear for instance.

The initial definition should be done in IB directly.
Select the TabBarItem in each UIViewController
Set the image in the Tab Bar Item section of Attributes Inspector.
Same thing for selected Image.
And you can define the default image (image) in bar Item as well.

If that does not work, please explain more in detail the problem.
If that's OK don't forget to mark the correct answer to close the thread.
I used above code mentioned by me in individual View Controller in ViewDidLoad method... But I got this error: "Directly modifying a tab bar managed by a tab bar controller is not allowed."
If you use this code it will fail.

Correct code is:
  • declare an IBOutlet for the tabBarItem in its respective VC

For instance in VC1:
Code Block
    @IBOutlet weak var tabBarItem1: UITabBarItem!  
  • In viewDidload of VC1:

Code Block
tabBarItem1.title = "Myanmar"
tabBarItem1.image = UIImage(systemName: "m.square.fill"),
tabBarItem1.selectedImage = UIImage(systemName: "m.square")
  • Do the same in other VCs

Once again, it is so easier to do it in IB…
Accepted Answer
You can modify the properties of each UITabBarItem:
Code Block
if let tabBarItem1 = self.tabBarController?.tabBar.items?[0] {
tabBarItem1.title = "Myanmar"
tabBarItem1.image = UIImage(systemName: "m.square.fill")
tabBarItem1.selectedImage = UIImage(systemName: "m.square")
}
if let tabBarItem2 = self.tabBarController?.tabBar.items?[1] {
tabBarItem2.title = "Singapore"
tabBarItem2.image = UIImage(systemName: "s.square.fill")
tabBarItem2.selectedImage = UIImage(systemName: "s.square")
}
if let tabBarItem3 = self.tabBarController?.tabBar.items?[2] {
tabBarItem3.title = "Hyderabad"
tabBarItem3.image = UIImage(systemName: "h.square.fill")
tabBarItem3.selectedImage = UIImage(systemName: "h.square")
}


Changing UITabBar item image on selected and unselected state for Three Tabs but getting error
 
 
Q