how to set image in a tab bar item in swift?

change tab bar item title programmatically swift
change tab bar item image programmatically swift
custom tab bar item image
how to select tab bar item programmatically in swift
how to change tab bar item title font swift 4
tab bar item selected image storyboard
ios tab bar icon generator
set tab bar image color swift

I have taken a view controller & embedded it in a navigation Controller and again this has been embedded in a tab bar controller. when i am trying to set a image via story board,the image does not appear on a tab bar image name is 25.

please suggest me what can I do? how can I do it programmatically? what should I take proper image size for this purpose? Thanks in advance.

In your MainTabbarViewController

Bind the outlet of your tabbar:

@IBOutlet weak var myTabBar: UITabBar?

 override func viewDidLoad() {

      myTabBar?.tintColor = UIColor.white
      tabBarItem.title = ""



set the tabbar items here defined method below:

func setTabBarItems(){

      let myTabBarItem1 = (self.tabBar.items?[0])! as UITabBarItem
      myTabBarItem1.image = UIImage(named: "Unselected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
      myTabBarItem1.selectedImage = UIImage(named: "Selected ")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
      myTabBarItem1.title = ""
      myTabBarItem1.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)

      let myTabBarItem2 = (self.tabBar.items?[1])! as UITabBarItem
      myTabBarItem2.image = UIImage(named: "Unselected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
      myTabBarItem2.selectedImage = UIImage(named: "Selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
      myTabBarItem2.title = ""
      myTabBarItem2.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)

      let myTabBarItem3 = (self.tabBar.items?[2])! as UITabBarItem
      myTabBarItem3.image = UIImage(named: "Unselected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
      myTabBarItem3.selectedImage = UIImage(named: "Selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
      myTabBarItem3.title = ""
      myTabBarItem3.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)

      let myTabBarItem4 = (self.tabBar.items?[3])! as UITabBarItem
      myTabBarItem4.image = UIImage(named: "Unselected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
      myTabBarItem4.selectedImage = UIImage(named: "Selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
      myTabBarItem4.title = ""
      myTabBarItem4.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)



Changing tab bar item image and text color iOS, Swift 3. I did it by creating a custom tabbar controller and added this code inside the viewDidLoad method. if let count = self.tabBar.items?.count { for i in 0. Now we have to customise our Tab bar items, for that, select the tab bar item at the bottom of the each view controller and go to the Utility panel. Open attribute inspector (4th tab from the left side) set the title and icon image of your view controllers.

add AppDelegate class :

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
    window=UIWindow(frame: UIScreen.main.bounds)
    self.window?.rootViewController = setTabbar()
    return true

func setTabbar() -> UITabBarController
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let tabbarcntrl = UITabBarController()

    let Home = storyboard.instantiateViewController(withIdentifier: "HomeView") // 1st tab bar viewcontroller
    let Followed = storyboard.instantiateViewController(withIdentifier: "FollowedView") // 2nd tab bar viewcontroller
    let Message = storyboard.instantiateViewController(withIdentifier: "MessageView") // 3rd tab bar viewcontroller

    // all viewcontroller embedded navigationbar
    let nvHome = UINavigationController(rootViewController: Home)
    let nvFollowed = UINavigationController(rootViewController: Followed)
    let nvMessage = UINavigationController(rootViewController: Message)

    // all viewcontroller navigationbar hidden
    nvHome.setNavigationBarHidden(true, animated: false)
    nvFollowed.setNavigationBarHidden(true, animated: false)
    nvMessage.setNavigationBarHidden(true, animated: false)

    tabbarcntrl.viewControllers = [nvHome,nvFollowed,nvMessage]

    let tabbar = tabbarcntrl.tabBar
    tabbar.barTintColor =
    tabbar.backgroundColor =
    tabbar.tintColor = UIColor(red: 43/255, green: 180/255, blue: 0/255, alpha: 1)

    //UITabBar.appearance().tintColor = UIColor.white
    let attributes = [NSFontAttributeName:UIFont(name: "Montserrat-Light", size: 10)!,NSForegroundColorAttributeName:UIColor.white]
    let attributes1 = [NSFontAttributeName:UIFont(name: "Montserrat-Light", size: 10)!,NSForegroundColorAttributeName:UIColor(red: 43/255, green: 180/255, blue: 0/255, alpha: 1)]

    UITabBarItem.appearance().setTitleTextAttributes(attributes, for: .normal)
    UITabBarItem.appearance().setTitleTextAttributes(attributes1, for: .selected)

    let tabHome = tabbar.items![0]
    tabHome.title = "Home" // tabbar titlee
    tabHome.image=UIImage(named: "icon_home.png")?.withRenderingMode(.alwaysOriginal) // deselect image
    tabHome.selectedImage = UIImage(named: "icon_home.png")?.withRenderingMode(.alwaysOriginal) // select image
    tabHome.titlePositionAdjustment.vertical = tabHome.titlePositionAdjustment.vertical-4 // title position change

    let tabFoll = tabbar.items![1]
    tabFoll.title = "Followed"
    tabFoll.image=UIImage(named: "icon_fold.png")?.withRenderingMode(.alwaysOriginal)
    tabFoll.selectedImage=UIImage(named: "icon_fold.png")?.withRenderingMode(.alwaysOriginal)
    tabFoll.titlePositionAdjustment.vertical = tabFoll.titlePositionAdjustment.vertical-4

    let tabMsg = tabbar.items![3]
    tabMsg.title = "Message"
    tabMsg.image=UIImage(named: "icon_mail.png")?.withRenderingMode(.alwaysOriginal)
    tabMsg.selectedImage=UIImage(named: "icon_mail.png")?.withRenderingMode(.alwaysOriginal)
    tabMsg.titlePositionAdjustment.vertical = tabMsg.titlePositionAdjustment.vertical-4

    return tabbarcntrl

How to Create A Custom Tab Bar Icons in IOS, You can click on tab bar icon to change your tabbar's image. image for in the storyboard in Xcode and set the 'Image' (under 'Bar Item' in the .com/questions/ 30429844/swift-and-xcode-how-to-create-custom-tab-bar-icons� You can also change the tag property of a tab bar item, which is helpful for identifying items. And last but not least, the UITabBarItem class has a few convenience initializers, such as init (title:image:selectedImage:). They let you set the properties of the tab bar item directly, when initializing the object.

Set both images- for select/selected state

iOS, If you want a built-in icon and title, set the 'System Item' to the corresponding value. For a custom icon, tabBarItem.image = [UIImage imageNamed:@"item"]; self. Swift:#. self.title = "item" self.tabBarItem.image = UIImage(named: "item") self. Set image in a tab bar item. GitHub Gist: instantly share code, notes, and snippets.

You are doing all the things in right way But the only problem is your tabbaritem image is not in correct size .Just look this table for actual size of tabbaritem images.

Customizing Tab Bar Controller Icons. – Make App Pie, You can use rendering mode to change to a tab bar image as an .original rendering mode. Tab bar items are stored within the view controller in the tabBarItem property, so under the line we just SquareViewController.swift� I am trying to set an Image for bar button Item for that I have an image like: with resolution 30 * 30 but while I assign this Image to Bar button Its looks like: I have assigned image this way : and If I try this way like making an IBOutlet for the button and set Image programatically form this question and code for that is:

In swift 4 and 5 you can use the below extension. Remember one thing always pass the same number of images , selected images and title but if you do not want to set title then pass nil in title.

extension UITabBarController{

    func setUpImagaOntabbar(_ selectedImage : [UIImage], _ image : [UIImage], _ title : [String]?){

        for (index,vals) in image.enumerated(){

            if let tab = self.tabBar.items?[index]{

                tab.image = image[index]
                tab.image = selectedImage[index]
                if let tile = title[index]{
                   tab.title = title[index]


Here is how to programmatically set icon / image for Tab Bar Item in , I also assume you have three tab bar items with individual view controllers and further you have created images assets with your icon images. Navigation Tab Bar in IOS | Swift tutorial Different Customizing techniques for Navigation bar with storyboard and code 1) Custom NavigationTabBar by StoryBoard 1.1) Add navigation bar directly by storyboard and Add single navigation bar button Set property Top bar : – Select the viewController and change the property of Tab bar to Trnaslucent Navigation Bar.

I'm setting a tab bar item's image programmatically and every time I , The profile tab's image is set programmatically. As you can see, I'm not setting the tab bar item image from the interface builder. I am currently adding images to each one of my tab bar items. One image I like has a size that is much bigger then the area of the bar item. When I set the image of the bar item in the storyboard and run the emulator the bar button with that image covers the entire screen.

Changing icon in a tab bar item, I'm trying to set the image for a tab bar item. I have declared it I have defined an image set in xcassets, with 3 images (size 24, 48 and 72). In bar item section:. The tab bar automatically obtains its items from the tab Bar Item property of each view controller associated with the tab bar controller. To configure tab bar items directly, use the set Items(_: animated:) method of the tab bar itself. A tab bar displays all of its tabs onscreen at once, using the item Positioning property to determine how to

UITabBarItem, You can also specify a badge value on the tab bar item for adding additional to create a custom item with the specified title, unselected image, and selected� Jump to navigation . How To Change Tab Bar Item Color In Swift Programmatically

  • NO, no need to do it prog.. It can be done via above method too.
  • But why this is not working on my project?
  • Can you show the output screenshot.
  • this is looking like the last image
  • May be this will help you
  • what is the use of tab bar outlet if you are not using it anywhere?
  • in AppDelegate class
  • Thanks for your answer :)
  • I did like you but same problem. Is it a bug or problem of my computer I could not understand. Is their any naming convention of image,image size?
  • when I am using icon from iconbeast or icons8 they are finely displayed but when I am using my image they are not supported. Any idea?
  • See This and check what was error in your code
  • when i downloaded image from image icon,its property shows 30x30
  • You need to create 1x , 2x , 3x of Tabbar icon.So Proper assets for your image are : 30x30 - 1x, 60x60 - 2x,90x90 - 3x
  • what does (About 75 x 75) means?