Changing tab bar item image and text color iOS

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

Here is my tab bar:

\"enter

The following image shows the program being run an

相关标签:
22条回答
  • 2020-12-02 07:06

    Swift 5.3

    let vc = UIViewController()
    vc.tabBarItem.title = "sample"
    vc.tabBarItem.image = UIImage(imageLiteralResourceName: "image.png").withRenderingMode(.alwaysOriginal)
    vc.tabBarItem.selectedImage = UIImage(imageLiteralResourceName: "image.png").withRenderingMode(.alwaysOriginal)
            
    // for text displayed below the tabBar item
    UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.black], for: .selected)
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-02 07:07

    This Code works for Swift 4 if you want to change the image of Tab Bar Item when pressed. Copy and paste in the first viewDidLoad method that is hit in the project

       let arrayOfImageNameForSelectedState:[String] = ["Image1Color", "Image2Color","Image3Color"]
       let arrayOfImageNameForUnselectedState: [String] = ["Image1NoColor","Image2NoColor","Image3NoColor"]
    
    
        print(self.tabBarController?.tabBar.items?.count)
    
        if let count = self.tabBarController?.tabBar.items?.count {
            for i in 0...(count-1) {
                let imageNameForSelectedState   = arrayOfImageNameForSelectedState[i]
                print(imageNameForSelectedState)
                let imageNameForUnselectedState = arrayOfImageNameForUnselectedState[i]
                print(imageNameForUnselectedState)
                self.tabBarController?.tabBar.items?[i].selectedImage = UIImage(named: imageNameForSelectedState)?.withRenderingMode(.alwaysOriginal)
                self.tabBarController?.tabBar.items?[i].image = UIImage(named: imageNameForUnselectedState)?.withRenderingMode(.alwaysOriginal)
            }
        }
    
    0 讨论(0)
  • 2020-12-02 07:08

    In Swift 5 ioS 13.2 things have changed with TabBar styling, below code work 100%, tested out.

    Add the below code in your UITabBarController class.

    override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
            let appearance = UITabBarAppearance()
            appearance.backgroundColor = .white
    
            setTabBarItemColors(appearance.stackedLayoutAppearance)
            setTabBarItemColors(appearance.inlineLayoutAppearance)
            setTabBarItemColors(appearance.compactInlineLayoutAppearance)
    
            setTabBarItemBadgeAppearance(appearance.stackedLayoutAppearance)
            setTabBarItemBadgeAppearance(appearance.inlineLayoutAppearance)
            setTabBarItemBadgeAppearance(appearance.compactInlineLayoutAppearance)
    
            tabBar.standardAppearance = appearance
     }
    
    @available(iOS 13.0, *)
    private func setTabBarItemColors(_ itemAppearance: UITabBarItemAppearance) {
        itemAppearance.normal.iconColor = .lightGray
        itemAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.gray]
    
        itemAppearance.selected.iconColor = .white
        itemAppearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.orange]
    }
    
    @available(iOS 13.0, *)
    private func setTabBarItemBadgeAppearance(_ itemAppearance: UITabBarItemAppearance) {
        //Adjust the badge position as well as set its color
        itemAppearance.normal.badgeBackgroundColor = .orange
        itemAppearance.normal.badgeTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
        itemAppearance.normal.badgePositionAdjustment = UIOffset(horizontal: 1, vertical: -1)
    }
    
    0 讨论(0)
提交回复
热议问题