How do I force UIBarButtonItem text to a specific color? - ios

I have a UIBarButtonItem on a navigation bar. When I enter my app, it starts out white (the desired color) and then switches abruptly to a faded out gray color. If I interact with the app and later return to the same view controller, the button returns to the white color. What could be causing this?
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Logout" style:UIBarButtonItemStyleDone target:self action:#selector(handleBack:)];
[backButton setTitleTextAttributes:[NSDictionary dictionaryWithObject: [UIColor whiteColor]
forKey: UITextAttributeTextColor] forState:UIControlStateNormal];
//[backButton setTintColor: [UIColor whiteColor]];
self.navigationItem.leftBarButtonItem = backButton;
[self.navigationController.navigationBar setBarStyle: UIBarStyleBlack];
[self populateSpinners];
isCategoryPicker = true;
[self.view setTintAdjustmentMode:UIViewTintAdjustmentModeNormal];
}
I've tried setting the tint color at the view level, the toolbar level, and at the button level and nothing seems to be working. Any help is appreciated!

Related

iOS - Change only backBarButtonItem text color on a navigation bar

I'm trying to change the color of the backBarButtonItem on a navigation bar in my app (iOS 9+).
I can do this with :
[[UIBarButtonItem appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName: [UIColor clearColor]} forState:UIControlStateNormal];
But this will change the color for all UIBarButtonItems on the navigation bar and I only want to change the back button.
I have tried :
[self.navigationItem.leftBarButtonItem setTitleTextAttributes:#{NSForegroundColorAttributeName: [UIColor clearColor]} forState:UIControlStateNormal];
and
[self.navigationItem.leftBarButtonItem setTitleTextAttributes:#{NSForegroundColorAttributeName: [UIColor clearColor]} forState:UIControlStateNormal];
But it doesn't work
NOTE: I want to keep the < arrow of the system back button so using a custom view is not an option unless I use a custom image
In the end I used the following solution:
1. Set the general appearance of UIBarbuttonitem BEFORE adding the right bar button item
[[UIBarButtonItem appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName: [UIColor blueColor]} forState:UIControlStateNormal];
2. Then set the right bar button item and its specific appearance
UIBarButtonItem *rigthBtn = [[UIBarButtonItem alloc] initWithTitle:#"title" style:UIBarButtonItemStylePlain target:self action:#selector(rightBtnTapped:)];
[rigthBtn setTitleTextAttributes:#{NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateNormal];
[self.navigationItem setRightBarButtonItem:rigthBtn];
As per my comment posting sample code
UIButton *useButton = [UIButton buttonWithType:UIButtonTypeCustom];
useButton.frame = CGRectMake(0, 0, 20,17);
useButton.layer.masksToBounds = NO;
useButton.layer.shadowOffset = CGSizeMake(3, 3);
useButton.layer.shadowRadius = 3;
useButton.layer.shadowOpacity = 0.1;
useButton.layer.shadowColor = [UIColor blackColor].CGColor;
useButton.backgroundColor = [UIColor clearColor];
useButton.tintColor = [UIColor whiteColor];
[useButton setImage:[UIImage imageNamed:#"image if any"] forState:0];
[useButton addTarget:self action:#selector(btnBackTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:useButton]];
`
One year too late but did you tried to set the tintColor? I dont know your syntax but in swift it would be:
navigationController.navigationBar.tintColor = UIColor.COLOR

NAvigation bar not appear

I want to make my NavigationbarClear color. So I tried like this in my ViewDidLoad
self.navigationController.navigationBar.hidden=NO;
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage=[UIImage new];
self.navigationController.navigationBar.translucent=YES;
self.navigationController.navigationBar.topItem.titleView.tintColor=[UIColor blackColor];
self.navigationController.navigationBar.topItem.title=[lan GetConvertedLanguageString:#"My Profile"];
self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"backarrow"] style:UIBarButtonItemStylePlain target:self action:#selector(revealToggle :)];
[self.navigationController navigationBar].tintColor = [UIColor whiteColor];
But nothing visible in my view controller. Whats wrong with this code? Please help me.
Thanks
Pick up navigation controller scene and then pickup navigationbar (see in pic) after that set the clear color from showutilites>attribute insepecter also you can change title color from here
Screenshot
Code
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage=[UIImage new];
self.navigationItem.title = #"1234";
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Left" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
[self.navigationController navigationBar].tintColor = [UIColor whiteColor];
So,I guess,your background of View is White,so you cannot see white text.
Note
navigationBar.titleTextAttributes
Display attributes for the bar’s title text.
navigationBar.tintColor
The tint color to apply to the navigation items and bar button items.

Why does my custom Navigation bar back button overlap/duplicated (Xcode 7.0)?

I added the following to customize my back button in my navigation bar but the image just overlaps/duplicated:
http://i.stack.imgur.com/eyzJC.png
- (void)viewWillAppear:(BOOL)animated
{
//take out the label following the button
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]initWithTitle:#"" style:UIBarButtonItemStylePlain target:nil action:nil];
//set image for back button
[[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:#"backButtonImage.png"]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:#"backButtonImage.png"]];
self.navigationItem.backBarButtonItem = backButton;
}
It's supposed to be an image of one black arrow facing to the left.
Before it worked fine but now it does this when I simulate it. Any insight and help would be appreciated.
Thank you
This should work for you.
UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
back.frame = CGRectMake(0, 0, 50, 44);
back.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[back addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
[back setImage:[UIImage imageNamed:#"backButtonImage.png"] forState:UIControlStateNormal];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView: back];
add a function:
-(void) back
{
[self.navigationController popViewControllerAnimated:YES];
}
Add this code
[UINavigationBar appearance].barTintColor = [UIColor colorWithWhite:1.00 alpha:1];
It should solve your problem.

Transition to UINavigationBar with transparent background image results in black flickering

I want to navigate from a white UINavigatinonBarto a transparent UINavigationBar.
In my root view controller, this is the setup:
self.navigationController.navigationBar.hidden = NO;
[self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];
[self.navigationController.navigationBar setTranslucent:NO];
It's important that it stays non translucent.
This is the setup in the second view controller:
// set title of navbar
self.title = [self.data objectForKey:#"title"];
[self.navigationController.navigationBar setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor whiteColor]}];
//
// IMPORTANT PART: make the nav bar transaparent - no prerequisites.
self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
self.navigationController.navigationBar.translucent = YES;
// set custom back button
UIButton *backButton = [[UIButton alloc] initWithFrame: CGRectMake(16, 31, 22, 20)];
UIImage *backImage = [UIImage imageNamed:#"backButtonDetail"];
[backButton setBackgroundImage:backImage forState:UIControlStateNormal];
[backButton setTitle:#"" forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(popBack) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backButtonItem;
This goes almost well, except for the animation:
As you can see, the background becomes black when I animate it. I want to fade the white into the destination color. It now animates from the black background.
I tried settings a custom UIView underneath the nav bar, but that didn't work.
I'm quite lost about how to about this now and any help would be highly appreciated.
I recently ran into this same problem and was able to get around it by just setting the window's background color in the appDelegate's didFinishLaunchingWithOptions
window.backgroundColor = UIColor.whiteColor()
and if you don't already have a reference to window then
UIApplication.sharedApplication().delegate?.window.backgroundColor = ...
I found this to be the cleanest solution. Hope it helps, albeit a bit late.

UIBarButtonItem tintColor stopped working

For some reason I can't use UINavigationBar on my view and I use UIToolBar instead. I was using dummy left button with clear tintColor and title button with black tintColor to properly center the title and hide the dummy left bar button, but it's not working anymore and both the dummy left button and the title button get either the main tintColor or if I disable them, they become gray.
How can I make them ideally disabled as buttons and set the proper tintColor (clear and black)?
- (void)addToolbar
{
CGFloat barHeight = 44.0;
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(self.view.bounds.origin.x, self.view.bounds.size.width - barHeight, self.view.bounds.size.height, barHeight)];
UIBarButtonItem *flexibleSpaceButtonItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil action:nil];
// Done button on the right
UIBarButtonItem *doneButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(done)];
// dummy button on the left to have centered title
UIBarButtonItem *dummyButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:nil action:nil];
dummyButtonItem.enabled = NO;
dummyButtonItem.tintColor = [UIColor clearColor];
// title button
UIBarButtonItem *titleButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Title text here" style:UIBarButtonItemStylePlain target:nil action:nil];
titleButtonItem.enabled = NO;
titleButtonItem.tintColor = [UIColor blackColor];
// set toolbar items
toolbar.items = #[dummyButtonItem,flexibleSpaceButtonItem, titleButtonItem, flexibleSpaceButtonItem,doneButtonItem];
[self.view addSubview:toolbar];
}
When you specify a tint for a view, the tint is automatically propagated to all subviews in the view’s hierarchy. Because UIWindow inherits from UIView, you can specify a tint color for the entire app by setting the window’s tint property using code like this:
Under application:didFinishLaunchingWithOptions:
window.tintColor = [UIColor purpleColor];
Source - iOS Transitions . Check it out under Using Tint Color section
Based on your feedback I found that by doing this in AppDelegate:
self.window.tintColor = [UIColor colorWithRed:255/255.0 green:59/255.0 blue:48/255.0 alpha:1.0];
[[UITextField appearance] setTintColor:[UIColor colorWithRed:66/255.0 green:107/255.0 blue:242/255.0 alpha:1.0]];
the second line causes my issue. I now need to find out why did I put the second line there :). Thanks for your help.

Resources