Swift change color of NavigationControl and TabBar

5k views Asked by At

I wanna change the color of the back button of the NavigationControl of all the app, how can I do it? I want to have it in red instead of the normal blue button...

And I have a problem with my TabBar. I have changed the icons color and name from standard blue to red with these:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.redColor()], forState:.Selected)

And this

class TabBarController: UITabBarController {

var color = UIColor.redColor()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    self.tabBarController?.tabBar.selectedImageTintColor = UIColor.redColor()
    UITabBar.appearance().selectedImageTintColor = UIColor.redColor()

But I have more than 5 tabs, so I have the button "More", and when I press it, the icons aren't red but blue, and when I press to edit the tab bar icons, the name of the tab is in red, but the icon not. What can I do? Picture to explain: http://postimg.org/image/67oqa15ll/

2

There are 2 answers

14
Beslan Tularov On

try this in your firstViewController

class ViewController: UIViewController,UITabBarDelegate, UITabBarControllerDelegate, UINavigationControllerDelegate {

        override func viewDidLoad() {
            super.viewDidLoad()

            UITabBar.appearance().tintColor = UIColor.redColor()
            var view: UITableView = self.tabBarController?.moreNavigationController.topViewController.view as UITableView
            view.tintColor = UIColor.redColor()
            if view.subviews.count != 0 {
                for cell in view.visibleCells() {
                    cell.textLabel??.textColor = UIColor.redColor()
                }
            }
        }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

and this in your UITabbarController

class TabbarViewController: UITabBarController,UITabBarDelegate, UITabBarControllerDelegate, UINavigationControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func tabBar(tabBar: UITabBar, didBeginCustomizingItems items: [AnyObject]) {
        self.view.tintColor = UIColor.redColor()

    }
}

Example Project https://www.dropbox.com/s/kbm4l60cnvyrf5h/UItabbarCustomizing.zip?dl=0

I hope I helped you

2
MatLaMEnacE On

To change text and Icon's color for all UITabBarItems throughout the application (in Swift / Xcode 6) :

In the AppDelegate.swift :

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    // Override point for customization after application launch.

    UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: your_color ], forState: .Selected)

    UITabBar.appearance().tintColor = your_color

    return true

}

And that will do the trick !