1

I have UIToolbar (inside my UINavigationController) with some UIBarButtonItems on it.

Toolbar is blue (isTranslucent = false), button is white. Default highlight color is gray and it looks ugly.

The only solution I found is to change UIBarButtonItem's tintColor in @IBAction touch event. But I have many UIBarButtonItems and its pretty annoying to write a plenty of code in every @IBAction.

Do you know how to change it globally or, at least, subclass UIBarButtonItem to define it once and use this class everywhere?

I use swift 3, deployment target is iOS 9+

John Kakon
  • 2,531
  • 4
  • 24
  • 28
  • 1
    You may add UIButton in side barbutton item then you may can manage that. – Nitin Gohel Aug 30 '17 at 13:57
  • Maybe you are looking to do something like subclass `UIButton`, override the touch events, and then call the super? https://stackoverflow.com/questions/27151246/uibutton-subclass-ibaction-not-working –  Aug 30 '17 at 14:24
  • @dfd something like this https://stackoverflow.com/a/43044276/2193926 but for `UIBarButtonItem`, not for `UIButton` – John Kakon Aug 30 '17 at 14:27
  • Not sure if this helps any better, but if you want to override `touchesBegan`, etc., they are part of `UIToolbar`. –  Aug 30 '17 at 14:33
  • @dfd I can't do it in `UIToolbar` because I need to highlight each `UIBarButtonItem` on toolbar separately. If I change `UIToolbar`'s TintColor all my buttons will change color, not the only one selected. – John Kakon Aug 30 '17 at 14:38
  • Ah. My bad. (That's what the deleted answer suggested, and I knew it wouldn't work.) I agree. My last thought - and a lot of coding - is to somehow (a) subclass `UIToolbar` or create your own to (b) use a subclassed `UIButton` with overridden touch events. Whatever you do, good luck. –  Aug 30 '17 at 14:44

3 Answers3

5

You have your didFinishLaunchingWithOptions function in your AppDelegate witch tells the delegate that the launch process is almost done and the app is almost ready to run.

And in there you can use the appearance on your UIBarButtonItem and UINavigationBar.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Text
    UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.green], for: .normal)
    UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.darkGray], for: .selected)

    // Images
    UINavigationBar.appearance().tintColor = UIColor.orange

    return true
}

Result:
enter image description here

Rashwan L
  • 38,237
  • 7
  • 103
  • 107
  • It looks exactly what I need, but it does nothing in my application. I don't know why. I create my items like this `let button = UIBarButtonItem(image_name: buttonIconName, title: buttonTitle, target: target, action_selector: buttonAction)` `self.setToolbarItems([flexible_space, button, flexible_space], animated: false)` – John Kakon Sep 04 '17 at 08:50
  • It works with "text only" buttons. Thats works: `let button = UIBarButtonItem(title: "test_text", style: .plain, target: self, action: nil)` Thats doesn't: `let button = UIBarButtonItem(image_name: "test_image", title: "test_text", target: self, action_selector: nil)` – John Kakon Sep 04 '17 at 09:02
  • You mean that it doesn't work with images @JohnKakon? – Rashwan L Sep 04 '17 at 09:04
  • oh, I found `extension UIBarButtonItem` in my code that make it work wrong =) my mistake, sorry. Your code was correct. – John Kakon Sep 04 '17 at 09:12
3

You can use appearance to change color globally. Add next lines in didFinishLaunchingWithOptions method

UIBarButtonItem.appearance().setTitleTextAttributes(
    [NSForegroundColorAttributeName: UIColor.white], for: .selected)
UIBarButtonItem.appearance().setTitleTextAttributes(
    [NSForegroundColorAttributeName: UIColor.white], for: .highlighted)
Alladinian
  • 34,483
  • 6
  • 89
  • 91
Taras Chernyshenko
  • 2,729
  • 14
  • 27
0

Swift 4.2

UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIColor.green], for: .normal)
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIColor.darkGray], for: .selected)
Shubham Mishra
  • 1,303
  • 13
  • 24