how to change the color of backBarButtonItem? - iphone

In my application I want to change the color of bacBarButtonItem.
Is it possible to change the color? or I have to put an image of it.
and in case of image tell me the code how to put the image.

If you just want to change the color you can do it with this line of code.
[[UIBarButtonItem appearance] setTintColor:[UIColor redColor]];
Replace redColor with the following to adjust the color of the buttons:
colorWithRed:0/255.0 green:144/255.0 blue:200/255.0 alpha:1.0// pick your color using this
Be sure to put this in the view controller that pushes. Not the view controller where you want to see this back button color.

Praveen-K answer is right, but take in mind that you'll have to do it in every viewcontroller.
Starting at iOS5, Apple have introduced the "appearance" concept.
- (void)setBackButtonBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0) UI_APPEARANCE_SELECTOR;
In your case would be something like this
UIImage *image = [UIImage imageNamed:#"imageName.png"];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:image forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
But as I said, Praveen-K answer is ok and will work, but just to let you know for the future.

UIImage *image = [UIImage imageNamed:#"imageName.png"];
UIBarButtonItem* backBarButton = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:#selector(backButtonAction)];
self.navigationItem.leftBarButtonItem=backBarButton;
[backBarButton release];

Another way to change the color of back bar button item is to use segment control
UISegmentedControl *button = [[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Back", nil]] autorelease];
button.frame = CGRectMake(0, 0, 60, 30);
button.center = self.view.center;
button.momentary = YES;
button.segmentedControlStyle = UISegmentedControlStyleBar;
button.tintColor = [UIColor colorWithRed:0 green:0.1 blue:0.5 alpha:0];
[button addTarget:self action:#selector(handleBack:) forControlEvents:UIControlEventValueChanged];
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
Notice that we assign the color we want to the property tintColor of UISegmentedControl.
I got the idea from this site:
http://charles.lescampeurs.org/2011/02/10/tint-color-uibutton-and-uibarbuttonitem

Related

customize UIBarButtonItem initWithBarButtonSystemItem to show up as grey not white in iOS 5

I am using a white background on my UINavigationBar with darkGrayColor text
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"uinavigationcontrollerbackground"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:font,UITextAttributeFont ,[UIColor darkGrayColor], UITextAttributeTextColor, [UIColor whiteColor], UITextAttributeTextShadowColor, nil]];
Which looks nice but I have buttons defined like this throughout my app:
self.searchButton =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:#selector(searchProducts)];
I killed the default blakc background look I had going like this:
[[UIBarButtonItem appearance] setBackgroundImage:[UIImage imageNamed:#"uibarbuttonbackground"] forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
Which is just a white square.
Now my icons are still white with a light gray shadow, anyway to change these default icons?
Once I tried to change background of UIBarButtonSystemItemCamera but nothing worked. I guess these default system button types (UIBarButtonSystemItem )come with a background image already, so changing it won't do.
I recommend you to create a custom view and init your bar button with it. Not a perfect code but I hope it helps.
UIImage *image = [UIImage imageNamed:#"icon.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage: [image stretchableImageWithLeftCapWidth:7.0 topCapHeight:0.0] forState:UIControlStateNormal];
button.frame= CGRectMake(0.0, 0.0, image.size.width, image.size.height);
[button addTarget:self action:#selector(takePicture:) forControlEvents:UIControlEventTouchUpInside];
UIView *v=[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, image.size.width, image.size.height) ];
[v addSubview:button];
UIBarButtonItem *barItem = [[[UIBarButtonItem alloc] initWithCustomView:v] autorelease];
I used the category solution found here, it is working great:
UIBarButtonItem with custom image and no border
I little more work than I was hoping but it is a nice clean solution.

iPhone Set Tint Color of Back Bar Button Item

I am trying to set the tint color of the back button within a navigation controller, but nothing is working. I have tried
[self.navigationController.backBarButtonItem setTintColor:myColor];
//myColor was previously set
But it stays the default tint
I believe barButtonItem is read-only. (I'm not too sure about this, someone correct me if I'm wrong)
To give a tint colour to these buttons, this is what I would do:
In your App Delegate, add these lines of code:
UIBarButtonItem *barButtonAppearance = [UIBarButtonItem appearance];
[barButtonAppearance setTintColor:[UIColor redColor]]; // Change to your colour
The first line sets an appearance proxy, so I believe this works too, if you like less code:
[[UIBarButtonItem appearance] setTintColor:[UIColor redColor]];
I hope this works for you! (This changes all instances of UIBarButtonItem)
You can't change the tint color of UINavigationItem directly. You have to change the tint color of navigation bar and your bar button will automatically change its color.
Add these lines in your viewWillAppear method
[[self.navigationController navigationBar] tintColor] = [UIColor myColor];
Or You can create a custom button and initialize your UIBarButtonItem as
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:yourUIButton];
This worked for me.
[self.navigationController.navigationBar setTintColor:[UIColor redColor]];
It is very similar to the second answer...
If you prefer to do it in Interface Builder without writing code:
Select your UINavigationBar (inside UINavigationController)
In Attributes Inspector, find "Tint" - this is what sets Back Button color. Note that "Bar Tint" is not the same - this sets Navigation Bar background color.
It is important to set Back Button appearance this way, and not try to implement leftBarButtonItem, because implementing leftBarButtonItem will disable the built-in back navigation gestures, like swipe to go back.
Swift way:
It changes all items in the nav.
In AppDelegate do something like this:
let navControl = UINavigationBar.appearance()
navControl.tintColor = UIColor.grayColor()
Well you can certainly change the color of just the Back Button or perhaps any Bar Button on the Navigation bar. Here's a small snippet to do it.
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStylePlain target:self action:nil];
backButton.tintColor = [UIColor blackColor];
[self.navigationItem setBackBarButtonItem:backButton];
[backButton release];
Well this is just one of the ways for doing this.
I hope this helps!! I know the answer for it is already accepted, but I thought to give a proper snippet to it.
Cheers!!! Happy Coding!!
If you want to set it to predefined style you can use
[navigationBar setBarStyle: UIBarStyleBlack];
Place the following code in AppDelegate that will set color for Back button globally
//Set color of BackButtonItem globally
[[UIBarButtonItem appearance] setTintColor:[UIColor colorWithRed:5.0/255.0
green:127.0/255.0 blue:173.0/255.0 alpha:1.0]];
This is supported in iOS 5 and above
It always work for me:
self.navigationItem.leftBarButtonItem = [self logicToAddBackButton];
GET DEFAULT BACK BUTTON
-
(UIBarButtonItem*) logicToAddBackButton
{
UIImageView *imageView;
// [imageView setTintColor:[UIColor redColor]];
UILabel *label=[[UILabel alloc] init];
if (WHITEBACK) {
imageView =[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"UiNavigationBackIPAD"]];
[label setTextColor:[UIColor whiteColor]];
}else{ //DEFAULTBACK
imageView =[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"UiNavigationBack"]];
[label setTextColor:[UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0]];
}
[label setText:#"Back"];
[label sizeToFit];
int space=6;
label.frame=CGRectMake(imageView.frame.origin.x+imageView.frame.size.width+space,
label.frame.origin.y, label.frame.size.width,
label.frame.size.height);
UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, label.frame.size.width+imageView.frame.size.width+space,
imageView.frame.size.height)];
view.bounds=CGRectMake(view.bounds.origin.x+8, view.bounds.origin.y-1, view.bounds.size.width,
view.bounds.size.height);
UIButton *button=[[UIButton alloc] initWithFrame:CGRectMake(view.bounds.origin.x, view.bounds.origin.y,
view.bounds.size.width, view.bounds.size.height)];
button.bounds=CGRectMake(view.bounds.origin.x, view.bounds.origin.y, view.bounds.size.width,
view.bounds.size.height);
[button addTarget:self action:#selector(eventBack) forControlEvents:UIControlEventTouchUpInside];
[button addSubview:imageView];
[button addSubview:label];
[UIView animateWithDuration:0.33 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
label.alpha = 0.0;
CGRect orig=label.frame;
label.frame=CGRectMake(label.frame.origin.x+25, label.frame.origin.y -5, label.frame.size.width,
label.frame.size.height+10);
label.alpha = 1.0;
label.frame=orig;
} completion:nil];
UIBarButtonItem *backButton =[[UIBarButtonItem alloc] initWithCustomView:button];
return backButton;
}
BACK BUTTON ACTION
(void)eventBack
{
[self.navigationController popViewControllerAnimated:YES];
}
UiNavigationBack Image (Please change colour of image as require with same size [25X41 144pixels/inch] to get
default look)
What worked for me was this :
self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor textPrimaryColor] forKey:NSForegroundColorAttributeName];

Is there any way to change text color of UIBarButtonItem?

I want to change text color of UIBarButtonItem,like Pinterest app(navigationItem.backBarButtonItem).
But here's almost same question.
Is there an easy way to change the text color of a UIBarButtonItem without using an image?
So,Why text color of UIBarButtonItem is changed in Pinterest app?
Try setTitleTextAttributes:forState:
You can learn more from here
#interface MyViewCOontroller : UIViewController {
UILabel *lblTotCaratteri;
}
In implementation
UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
lblTotCaratteri = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 25, 15)];
lblTotCaratteri.textAlignment = UITextAlignmentCenter;
lblTotCaratteri.textColor =[UIColor greyColor];
hope this helps
You can use UISegmentedControl to simulate the UIBarButtonItem. Make sure you have a segment control with single segment.
- (void) viewDidLoad {
[YOUR_SEGMENTED_CONTROL removeAllSegments];
[YOUR_SEGMENTED_CONTROL insertSegmentWithTitle:#"MyButton" atIndex:0 animated:NO];
}
Retrieve the label of the UISegmentedControl and set the textColor to your preferred color
UILabel* lblText = [[[[YOUR_SEGMENTED_CONTROL subviews] objectAtIndex:0] subviews] objectAtIndex:0];
lblText.textColor = [UIColor blackColor];
I don't the direct way of doing this but you can achieve this by adding a UIBUtton to the customview of the UIBarButtonItem.
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(0, 0, 80, 40);
[backButton setBackgroundImage:[UIImage imageNamed:#"back-button.png"] forState:UIControlStateNormal];// Setting the background Image
[backButton setTitle:#"Back" forState:UIControlStateNormal];//Setting the title of the button
[backButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];// Setting the text color.
[backButton addTarget:self action:#selector(backButtonTaped) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backButtonItem;
And this will give you a UIBarButtonItem with the text "Back" in red color.

Is there an easy way to change the text color of a UIBarButtonItem without using an image?

Is there an easy way to change the text color of a UIBarButtonItem without using an UIImage as the background?
By default the text is always white. I'd like to make it black.
Thank to IOS 5.0 you do not need to use images for that.You can set various text attributes with following code.
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:#"Title" style:UIBarButtonItemStyleBordered target:nil action:nil];
if ([button respondsToSelector:#selector(setTitleTextAttributes:forState:)]) {
NSDictionary *textAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
kTextColor,UITextAttributeTextColor,
nil];
[[UIBarButtonItem appearance] setTitleTextAttributes:textAttributes
forState:UIControlStateNormal];
}
I'd rather use a UIButton inside a UIBarButtonItem and customize that one.
This is an example with custom graphics for a custom UIButton. The idea stays the same use initWithCustomView of the UIBarButtonItem to put something else in it which is easily customizable.
self.closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[closeButton setImage:[UIImage imageNamed:#"webview_close_button_normal.png"] forState:UIControlStateNormal];
[closeButton setImage:[UIImage imageNamed:#"webview_close_button_pressed.png"] forState:UIControlStateHighlighted];
closeButton.frame = CGRectMake(0, 0, 121, 36);
[closeButton addTarget:self action:#selector(closeAdViewController) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem * aBarButtonAdClose = [[[UIBarButtonItem alloc] initWithCustomView:closeButton] autorelease];
You can set a custom view by using
[[UIBarButtonItem alloc] - (id)initWithCustomView:(UIView *)customView]
The direct answer to your question is no.
You can't simply change the color of a UIBarButtonItem by setting one of its properties. You can set the tintColor property of the UIToolBar or UINavigationBar that contains the UIBarButtonItems, but that also affects the color of the toolbar itself and doesn't offer too much customization.
If that doesn't work for you, a custom view as the other answers suggest is a fine idea.
Good luck!

Change color of UIBarButtonSystemItemCancel

If I use the UIBarButtonSystemItemCancel, it's color is set to that of the navigation bar. However, in the Photos App, the "Cancel" button is shown with a blue background. How can I also set the UIBarButtonSystemItemCancel to have a blue background please?
your can use image as button image as
UIImage *buttonImage = [UIImage imageNamed:#"image.png"];
//create the button and assign the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
//create a UIBarButtonItem with the button as a custom view
//UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[button addTarget:self action:#selector(clickActionItem:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button ];
self.navigationItem.rightBarButtonItem = rightBarButtonItem;
set the theNavigationController.navigationBar.barStyle to UIBarStyleBlackTranslucent
theNavigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
and the buttons will be blue by default.
There is a better way than using an image. The UISegmentedControl can be configured to look just like a UIBarButtonItem and UISegmentedControl has a tintColor property.
UISegmentedControl *button = [[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:itemTitle, nil]] autorelease];
button.momentary = YES;
button.segmentedControlStyle = UISegmentedControlStyleBar;
button.tintColor = color;
[button addTarget:theTarget action:selector forControlEvents:UIControlEventValueChanged];
UIBarButtonItem *removeButton = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
I built a UIBarButtonItem category that'll do this.