UINavigationBar styling without background images - iphone

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]];

Related

Customize the tabbar and tabbar item

I am using UITabbarController in my application and also I set the frame to it so that tabbar is display in center. I am so change color and image of tabbar but I want to remove the small edge its not clear how can i clear that color become white.
Code: [[UITabBar appearance] setShadowImage : [UIImage imageNamed:#"white.png"]];
Another option is, add
#import <QuartzCore/QuartzCore.h>
FrameWork and use
self.tabBarController.tabBar.layer.borderWidth = 0.50;
self.tabBarController.tabBar.layer.borderColor = self.tabBarController.tabBar.tintColor.CGColor;
add clipsToBounds and set to YES.
[[UITabBar appearance] setShadowImage:[UIImage imageNamed:#"transparentImage.png"]];
Try this with a transparent image.
or
[[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
[[UITabBar appearance] setBackgroundImage:[[UIImage alloc] init]];
These all are responsible for it. Try with second approach, as first is not working for you.

How can I adjust the highlight color of my UIBarButtonItem?

I am using the appearance proxy to adjust my color settings globally:
//Setup custom appearances
if ([UINavigationBar respondsToSelector:#selector(appearance)]) {
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"header"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setTintColor:[UIColor colorWithRed:96.0/255.0 green:13.0/255.0 blue:11.0/255.0 alpha:1.0]];
[[UIBarButtonItem appearance] setTintColor:[UIColor colorWithRed:96.0/255.0 green:13.0/255.0 blue:11.0/255.0 alpha:1.0]];
//rgb: 96, 13, 11
//[[UIToolbar appearance] setBackgroundImage:[UIImage imageNamed:#"header"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
[[UIToolbar appearance] setTintColor:[UIColor blackColor]];
}
This works fine, but when I tap on a UIBarButtonItem that is in my UINavigationbar, it changes color to black. How can I make sure the highlighted state is not black?
last line is:
[[UIToolbar appearance] setTintColor:[UIColor blackColor]];
maybe rem it?
Check this out: UIBarButtonItem with color?
Make sure that you import "QuartzCore/QuartzCore.h" in order to access the layer properties of UIView. To import that, you need to have added the CoreGraphics framework to your project.

back navigationItem with title & background image

I know how to add back navigation button with image or change the tile but I haven't seen any example with background image and its title (#"back").
Can I add a custom UIButton with custom background image and title?
This, what I finally did in my code actually works!!!
Just update "forState" and "barMetrics" values for different appearances.
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[UIImage imageNamed:#"myImage"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
I'm not sure what you mean by (#back) but here is how I was able to get an image in the back button and the the UINavigationBar:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"myLogo.png"] forBarMetrics:UIBarMetricsDefault];
UIImage *backButton = [[UIImage imageNamed:#"HeaderTest.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(12, 12, 12, 12)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButton forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
This is an ios 5 solution. Thank you to these sources:
http://www.whypad.com/posts/ios-5-problem-setting-image-for-uinavigationbar/1011/
http://iosdevelopertips.com/user-interface/ios-5-customize-uinavigationbar-and-uibarbuttonitem-with-appearance-api.html

MPMoviePlayerViewController vs. Appearance-Framework (iOS 5)

i am using a MPMoviePlayerViewController inside my Project. I have defined the following appearance for UIButtons and UINavigationbar like so:
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil]
setBackgroundImage:[UIImage imageNamed:#"woodenBarButton"]
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"navigationbarBackground"]
forBarMetrics:UIBarMetricsDefault];
So now MPMoviePlayerViewController is also using a navigation bar. The problem is now: The button uses the given appearance but the toolbar not => it looks crappy. So now i have the following questions:
Can i convince the UINavigationBar in MPMoviePlayerViewController to use the appearance?
OR can is remove appearance completely only for the MPMoviePlayerViewController.
I tried to access the button via playerViewController.navigationitem.leftbarbuttonitem and overwrite properties but that doesn't work.
Would be great if you can help me. Thanks.
have you tried also setting appearanceWhenContainedIn for MPMoviePlayerViewController? appearance and appearanceWhenContainedIn works both at the same time.
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"navigationbarBackground"]
forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearanceWhenContainedIn:[MPMoviePlayerViewController class], nil] setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];

Change UINavigationItem colour

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]];
}