Globally change UIBarButtonItem text color when selected or highlighted

uibutton change background color when pressed swift
swift highlight button when pressed
swift change button color when pressed
uibutton highlight color programmatically
uibutton background color not changing swift
uibutton selected and unselected swift
swift change background color programmatically
uibutton selected color

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+

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:

Extending UIButton in Swift with Background Color for State, A UIButton can be in many states: default, selected, highlighted, or disabled. For instance, you can change the title, font, text color, shadow color, image, import UIKit import ObjectiveC // Declare a global var to produce a Extending UIBarButtonItem; Extending UIButton with Background Color for State. Windows 10: How to change text and background colors. The intention of this post is to enable you to change the color of all the fields displayed by your Windows 10 screen (there will always be exceptions – icons, images, etc. – but they should be of minor importance).

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)

tintColor, The tint color to apply to the button item. Availability Although UIBarButtonItem is not a view, its tintColor property behaves the same as that of UIView . See the� If you wish to change the color for "selected text", press the Start Button and type "Windows colors and metrics" (without the " " marks). The list which appears above will contain a link to, Change windows colors and metrics. In the Item drop-down menu, choose Selected items.

Swift 4.2
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIColor.green], for: .normal)
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIColor.darkGray], for: .selected)

UIBarButtonItem, class UIBarButtonItem : UIBarItem value for the normal state so that other states without a custom value set can use it. Initializes a new item using the specified title, image, action, and context menu. The tint color to apply to the button item. A control that displays one or more buttons in a tab bar for selecting between� Change the highlighted color when cells are selected with VBA code. By default, when selecting a range of cells, the highlighted color is gray. If you want to make the selected range more standing out, you can change its highlighted color to the one you need. Please do as follows. 1.

UIBarButtonItem Class (UIKit), Constructor that allows specifying a title and style for a UIBarButtonItem along with an Constructor that allows a custom image, style and evnet handler to be coordinates for the path of an element that is used to highlight the element. The tint color to be used for the button. Title. The title that is displayed in the button. This feature is based on the Selected Items Color in Window Color settings. If you are using a Windows 7 operating system, you can change the text selection color via the steps below: 1. Right click on your desktop and select Personalize. 2. Select Window Color. 3. Click the "Item:" drop-down list. 4. Click "Selected Items" from the list and specify a color.

How to set the tint color of a UIView, The tintColor property of any UIView subclass lets you change the coloring for text views it means the selection cursor and highlighted text,� how to change windows 7 highlight color in aero theme (select file or folder)? This thread is locked. You can follow the question or vote as helpful, but you cannot reply to this thread.

Uibarbuttonitem with image and text swift, BlackTranslucent. system allows the global tint to be applied to your icon. done, The label example will automatically change the text color to black for light They show either a title or an image and change their appearance when pressed. This document, titled « Excel - Changing the highlighting color of selected cells », is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM ( ccm.net ).

Comments
  • You may add UIButton in side barbutton item then you may can manage that.
  • Maybe you are looking to do something like subclass UIButton, override the touch events, and then call the super? stackoverflow.com/questions/27151246/…
  • @dfd something like this stackoverflow.com/a/43044276/2193926 but for UIBarButtonItem, not for UIButton
  • Not sure if this helps any better, but if you want to override touchesBegan, etc., they are part of UIToolbar.
  • @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.
  • 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)
  • 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)
  • You mean that it doesn't work with images @JohnKakon?
  • oh, I found extension UIBarButtonItem in my code that make it work wrong =) my mistake, sorry. Your code was correct.