How can I change the font color of a UIBarButton item?
This question was answered here. Basically, you have to create a UIButton, configure it as you wish, and then initialize the Bar Button Item with the UIButton as a custom view.
How to change the font color / Text Color of the UIBarButtonItem on navigation bar
Another method is :-
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:@"delete.png"] forState:UIControlStateNormal];
[button setTitle:@"Delete" forState:UIControlStateNormal];
button.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:12.0f];
[button.layer setCornerRadius:4.0f];
[button.layer setMasksToBounds:YES];
[button.layer setBorderWidth:1.0f];
[button.layer setBorderColor: [[UIColor grayColor] CGColor]];
button.frame=CGRectMake(0.0, 100.0, 60.0, 30.0);
[button addTarget:self action:@selector(batchDelete) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem* deleteItem = [[UIBarButtonItem alloc] initWithCustomView:button];
Changing font color of UIBarButtonItem
What about using:
func setTitleColor(UIColor?, for: UIControlState)
Documentation says it sets the color of the title to use for the specified state.
sortButton.setTitleColor( .red, for: .normal)
How to change the font color / Text Color of the UIBarButtonItem on navigation bar
Another method is :-
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:@"delete.png"] forState:UIControlStateNormal];
[button setTitle:@"Delete" forState:UIControlStateNormal];
button.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:12.0f];
[button.layer setCornerRadius:4.0f];
[button.layer setMasksToBounds:YES];
[button.layer setBorderWidth:1.0f];
[button.layer setBorderColor: [[UIColor grayColor] CGColor]];
button.frame=CGRectMake(0.0, 100.0, 60.0, 30.0);
[button addTarget:self action:@selector(batchDelete) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem* deleteItem = [[UIBarButtonItem alloc] initWithCustomView:button];
Globally change UIBarButtonItem text color when selected or highlighted
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:
UIBarbuttonItem change the textColor
A UIBarButtonItem's color is its tintColor
. (Or it can use the tintColor
inherited from the toolbar you put it into.)
How to change tintColor of UIBarButtonItem in Swift?
The problem was that the button was automatically set as custom. I refigured it to system.
UIBarButtonItem programmatically change color
This is the way, I use text instead of icon. you can user FM symbols of fontawesome or any other. And also you can change the image when button click
class ViewController: UIViewController{
var btnTick:UIBarButtonItem?
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
btnTick = UIBarButtonItem(title: "2", style: .plain, target: self, action: #selector(didClickedBtn(_ :)))
btnTick?.tintColor = .blue
navigationItem.leftBarButtonItem = btnTick
}
@objc func didClickedBtn(_ sender : UIBarButtonItem){
sender.tintColor = .red
view.layoutIfNeeded()
}
}
Related Topics
How to Sort JSON Coming from Alamofire and Return Final JSON Object (Swiftyjson)
Deleting with One-To-Many Relationship
Swift Notification Fire from Datepicker
Content Blocker Extension with a String Instead of a File
How to Return a Button from a Function in Swiftui
Ckcontainer.Discoverallidentities Always Fails
What Is the Markup Format for Documentation on the Parameters of a Block in Swift
Remove Programmatically Added Uiimageview
How to Import Modules Without an Xcode Project in Swift
Typecast Unsafemutablepointer<Void> to Unsafemutablepointer<#Struct Type#>
What Might Be Causing This Animation Bug with Swiftui and Navigationview
Swiftui Tabbar: Action for Tapping Tabitem of Currently Selected Tab to Reset View
Swiftlint Overriding Project Settings Related to Spm
How to Apply a Context Menu to Buttons in a Swiftui List Row
Mpmusicplayercontroller Setqueuewithstoreids Playing Index