Minitour / AZTabBarController

A custom tab bar controller for iOS written in Swift 4.2
MIT License
348 stars 65 forks source link

How to change the selected TabBarItem background color #62

Closed StackHelp closed 4 years ago

StackHelp commented 4 years ago

I'm trying to change the background of a selected TabBarItem like below

InyGc

how can we achieve this?

Minitour commented 4 years ago

Have you tried

tabBarController.highlightedBackgroundColor = myColor

tabController.highlightButton(atIndex: 2) 
StackHelp commented 4 years ago

@Minitour let me check

Does tabController.highlightButton(atIndex: 2) needs to call on any delegate? Because this method fixed the color for the tab not changed to the initial color.

Fixed with this code. Any other solution you have?

    func tabBar(_ tabBar: AZTabBarController, didSelectTabAtIndex index: Int) {
        print("didSelectTabAtIndex \(index)")
        for (i, _) in tabController.buttons.enumerated() {
            if i == index {
                tabController.highlightButton(atIndex: i)
            } else {
                tabController.removeHighlight(atIndex: i)
            }
        }
    }
Minitour commented 4 years ago

I’m not sure I understand your desired functionality. Do you want the background to change when you click a button? Or just have a fixed background for one button like you showed in the picture?

StackHelp commented 4 years ago

Yes, Want to change the background color on click of tab item.

Minitour commented 4 years ago

Yeah I don’t remember implementing such functionality, your delegate workaround would do just fine.