Changing tab bar item image and text color iOS

前端 未结 22 1128
说谎
说谎 2020-12-02 06:10

Here is my tab bar:

\"enter

The following image shows the program being run an

22条回答
  •  Happy的楠姐
    2020-12-02 07:07

    Swift 3.0

    I created the tabbar class file and wrote the following code

    In viewDidLoad:

    self.tabBar.barTintColor = UIColor.white
    self.tabBar.isTranslucent = true
    
    let selectedColor   = UIColor.red
    let unselectedColor = UIColor.cyan
    
    UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: unselectedColor,NSFontAttributeName: UIFont(name: "Gotham-Book", size: 10)!], for: .normal)
    UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: selectedColor,NSFontAttributeName: UIFont(name: "Gotham-Book", size: 10)!], for: .selected)
    
    if let items = self.tabBar.items {
        for item in items {
            if let image = item.image {
                item.image = image.withRenderingMode( .alwaysOriginal )
                item.selectedImage = UIImage(named: "(Imagename)-a")?.withRenderingMode(.alwaysOriginal)
            }
        }
    }
    

    After viewDidLoad:

       override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
    
       if(item.title! == "title")
       {
        item.selectedImage = UIImage(named: "(Imagname)-a")?.withRenderingMode(.alwaysOriginal)
    
        }
        if(item.title! == "title")
        {
            item.selectedImage = UIImage(named: "(Imagname)-a")?.withRenderingMode(.alwaysOriginal)
    
        }
        if(item.title! == "title")
        {
            item.selectedImage = UIImage(named: "(Imagname)-a")?.withRenderingMode(.alwaysOriginal)
    
        }
        if(item.title! == "title")
        {
            item.selectedImage = UIImage(named: "(Imagname)-a")?.withRenderingMode(.alwaysOriginal)
    
        }
        if(item.title! == "title")
        {
            item.selectedImage = UIImage(named: "(Imagname)-a")?.withRenderingMode(.alwaysOriginal)
    
        }
    
    }
    

    in view did load method you have to set the selected image and other image are showing with RenderingMode and in tab bar delegate methods you set the selected image as per title

提交回复
热议问题