Change UINavigationItem colour - iphone

I need to set custom colors to my UINavigationBar buttons.
I'm doing the following thing(RGB func is a define):
- (void)viewWillAppear:(BOOL)animated
{
for (UIView *view in self.navigationController.navigationBar.subviews)
if ([[[view class] description] isEqualToString:#"UINavigationButton"])
[(UINavigationButton *)view setTintColor:RGB(22.0,38.0,111.0)];
}
Everything looks fine on app load. after leaving the view and getting back the color returns to default.
Secondly I need to set the same colour to UISegmentedControl to a pressed button.

[Change UINavigationItem colour](http://www.skylarcantu.com/blog/2009/11/05/changing-colors-of-uinavigationbarbuttons/"Changing colors of UINavigationBarButtons") and to set same color to UISegmentControl will help you to reach to your destination.
Here is a sample code for to set color to UISegmentControl.

Here's one way:
[[theNavigationBar.subviews objectAtIndex:1] setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[[theNavigationBar.subviews objectAtIndex:2] setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
However, HUGE, caveat. This is highly likely to break on a future OS release and is not recommended.
At the very least you should perform a lot of testing and make sure you your assumptions of the subview layout of the navigation bar are correct.
or
You can change the color by changing the tintColor property of the UINavigationBar
myBar.tintColor = [UIColor greenColor];
or
You can follow the below link
http://www.skylarcantu.com/blog/2009/11/05/changing-colors-of-uinavigationbarbuttons/

For IOS5 will have to use "setBackgroundImage:forBarMetrics:"
try this code to apply all UINavigationItem / UINavigationBar
if([[UINavigationBar class] respondsToSelector:#selector(appearance)]){ //iOS >=5.0
//use for UINavigationBar Custom image.
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageWithNamed:#"navImage.png"] forBarMetrics:UIBarMetricsDefault];
//use for UINavigationItem custom color
[[UINavigationBar appearance] setTintColor:[UIColor greenColor]];
}

Related

UINavigationBar styling without background images

I'm trying to find a way to modify the style of my UINavigationBar without using images. I want to create a flat UINavigationBar with a solid color. Is my only option to subclass UINavigationBar and draw the complete NavigationBar myself? I'm targeting iPhones running iOS 6.0.
you can use UINavigationBar appearance
here you go, edit rgb values of colors as you need
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundColor:[UIColor colorWithRed:1/256.0 green:96/256.0 blue:149/256.0 alpha:1.0]];
return YES;
}
Note: this wont effect bar button items , you have to look at [[UIBarButtonItem appearance] if you want to change buttons too
[[UIBarButtonItem appearance] setTintColor:[UIColor colorWithRed:126/256.0 green:181/256.0 blue:55/256.0 alpha:1.0]];

Navigation bar image black

In my application i add custom navigation image while adding the remaining part of the image shows black in color, i used following code,
UIImage *image = [UIImage imageNamed: #"navbar.png"];
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarStyleDefault];
i want to show the back ground color in the in the black color part.what should change in code?
Please Try Following may be it works fine.
UINavigationBar *navBar = [[self navigationController] navigationBar];
UIImage *backgroundImage = [UIImage imageNamed:#"navbar.png"];
[navBar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault];
Edited
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"navbar.png"] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"navbar.png"]];
I'm using very useful library (class SCNavigationBar) for styling navigaitonBar, download it here
It supports all orientations, it works with no problem in my many apps.

Hide Navigation Bar but not the back button

i hide my navigation using:
[self.navigationController setNavigationBarHidden:YES animated:YES];
But i need to not hide the back button, it's Possible?
nevan king is right but you can simply change the background image of the navigation bar or set it to nil. If you set it to nil or provide a transparent BG-image you would achieve the effect you need.
For iOS >= 5.0 you could simply set the appearance:
if([navigationBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)]) // needed if iOS older than 5.0 is also supported
[navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
You can do that where ever you have a pointer to your navigation bar. E.g. inside of the viewDidLoad method of your ViewController.
For older iOS version you need a workaround by making a category of UINavigationBar and overwrite the drawRect method:
#implementation UINavigationBar (BackgroundImage)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: #""];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
Both methods are compatible if you want to support all iOS versions.
Thus you should keep in mind, that the back button uses the same background image. So you will need to make a custom one.
UIImage *bgImageNormal = [UIImage imageNamed:#"backButtonImage.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage: bgImageNormal forState:UIControlStateNormal];
button.frame= CGRectMake(0.0, 0.0, bgImageNormal.size.width, bgImageNormal.size.height);
[button addTarget:self action:#selector(navigationBarBackButtonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside]; // your action method here
UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = closeButton;
[closeButton release];
This code needs to be implemented for each ViewController you are pushing to your navigation bar. A good place for it is also inside the viewDidLoad method.
The back button is created by the navigation bar and always part of it, so it's not possible. You could hide and re-show the navigation bar when your user touches on the screen (this is what the Photos app does when you look at a single photo) or create a button and have it permanently on the top left of the screen. You could also make the navigation bar partly transparent so that the content underneath shows up.

Transparent UIToolbar

I wrote the following code to make my toolbar transparent.
[mtoolbar setBackgroundColor:[UIColor clearColor]];
How do I make UIToolbar transparent?
You can set the property translucent to YES and see if this helps.
[self.toolbar setBackgroundImage:[UIImage new]
forToolbarPosition:UIToolbarPositionAny
barMetrics:UIBarMetricsDefault];
[self.toolbar setBackgroundColor:[UIColor clearColor]];
Setting the property translucent to YES will not work in iOS 5 and below. Here's how it can be done without subclassing toolbar:
const float colorMask[6] = {222, 255, 222, 255, 222, 255};
UIImage *img = [[UIImage alloc] init];
UIImage *maskedImage = [UIImage imageWithCGImage: CGImageCreateWithMaskingColors(img.CGImage, colorMask)];
[self.toolbar setBackgroundImage:maskedImage forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
Check the below code
[myToolbar setBarStyle:UIBarStyleBlack];
[myToolbar setTranslucent:YES];
Taken from
#Brandon Bodnár has answered in the below SO post.
Couldn't UIToolBar be transparent?
you could also use the different approach
Transparent UIToolBar
for (UIView * sv in [toolBar subviews])
{
[sv removeFromSuperview];
}
;) any iOs
The following works in iOS 5 (and iOS 6 beta 4, although a slight top shadow is still visible there).
Please note:
Making a UIToolbar or UINavigationBar transparent is rarely a good idea, and modifying Apple's UIKit elements in such a way is bound to break sooner or later.
TransparentToolbar.h
#import <UIKit/UIKit.h>
#interface TransparentToolbar : UIToolbar
#end
TransparentToolbar.m
#import "TransparentToolbar.h"
#implementation TransparentToolbar
-(void)insertSubview:(UIView *)view atIndex:(NSInteger)index
{
// This method is called with a view of class "UINavigationBarBackground" or "_UIToolbarBackground", respectively. It would be possible to check for this with NSStringFromClass([view class]) to be completely sure that we're skipping the right view.
if (index != 0)
{
[super insertSubview:view atIndex:index];
}
else
{
// insert your custom background view, if you want to
}
}
#end
EDIT: In iOS 5+, it's also possible to simply set the backgroundImage (which could be transparent). This is certainly the "cleaner" solution, but is less flexible than a custom UIView.
[someToolbar setBackgroundImage:[UIImage imageNamed:#"clear"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
This worked for me for iOS 6 and 7:
UIGraphicsBeginImageContextWithOptions(CGSizeMake(1, 1), NO, 0.0);
UIImage *blank = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self.toolBar setBackgroundImage:blank forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];

How to change the text color on UINavigationBar button items

I've got a UINavigationController and i've changed it to white using the Tint property of the navigation bar in Interface Builder. But the text in buttons and the title is still the default color, white, and so gets lost against the white background. Anyone know how to work around this?
As of iOS 5 this it much easier, using the UIBarButtonItem appearance proxy:
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor blackColor],
UITextAttributeTextColor,
[UIColor clearColor],
UITextAttributeTextShadowColor, nil];
[[UIBarButtonItem appearance] setTitleTextAttributes: attributes
forState: UIControlStateNormal];
Set this in application:didFinishLaunchingWithOptions:
for (id subView in theNavigationBar.subviews) {
if ([subView isKindOfClass:[UIButton class]]) {
[(UIButton *)subView setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[(UIButton *)subView setTitleShadowColor:[UIColor clearColor] forState:UIControlStateNormal];
}
}
Here's one way:
[[theNavigationBar.subviews objectAtIndex:1] setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[[theNavigationBar.subviews objectAtIndex:2] setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
However, HUGE, caveat. This is highly likely to break on a future OS release and is not recommended.
At the very least you should perform a lot of testing and make sure you your assumptions of the subview layout of the navigation bar are correct.
Or you use your own button bar item subclass with a setter you specify, lus isn't iPhone os 3 suppose to exposé text color for EVERY button
I did as drunknbass suggested. I resorted to making a series of images for back-button in a few states, and regular buttons in a few states, and put them in a custom UIButton subclass, setting up the appropriate styles.
As a solution it doesn't scale particularly well, but my needs were simple enough. It has the advantage of not breaking if the subview orders in the built in controls change though, which is the obvious downside of that approach. Disadvantage of needing several images. The other tricky thing is handling an orientation change, since the buttons should change size.
On a related note, if you do change the tint color of your navigation bar, it does not play well with the special "More" view controller for customizing the order of a tab bar. There doesn't seem to be any "acceptable" way to change the color of the toolbar in that view.
Can't you iterate the contents of the UINavigationBar view to find the buttons in the awakeFromNib?
Here the clean way (relying on public API) to do this IMHO : you should use the titleView property of the navigationItem to apply it your own UILabel on which you will customize the textColor
UILabel* lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
lbl.textAlignment=UITextAlignmentCenter;
lbl.backgroundColor=[UIColor clearColor];
lbl.textColor=[UIColor colorWithRed:0.18 green:0.2 blue:0.56 alpha:1];
theControllerOnWhichYouWantToHaveATitleWithYourOwnColor.navigationItem.titleView=lbl;
[lbl release];
you can change title appearance by adding label into the navigation bar like bellow.
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 35)];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = UITextAlignmentCenter;
label.textColor =[UIColor whiteColor];
label.text=self.title;
self.navigationItem.titleView = viewLabel;
[label release];