I am going to have different background image for navigation bar.(e.g.:when it is pushed)
i use this code
if([self respondsToSelector: #selector( presentingViewController)]){
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"sectionImage_iphone.png"] forBarMetrics:UIBarMetricsDefault];
}else{
[self.navigationController.navigationBar.layer.contents removeFromSuperlayer];
self.navigationController.navigationBar.layer.contents = (id)[UIImage imageNamed:#"sectionImage_iphone.png"].CGImage;
}
If i call it again does the background image is replaced or stacked? if stacked, how to remove it?(for ios 4 and 5)
Thank You
Related
In my iPhone app i have to change the UINavigationBar image on Button click.
so following is the function am calling on buttoclick
-(void)btnBlueDelegate{
// Create resizable images
UIImage *gradientImage44 = [[UIImage imageNamed:#"header_Blue.png"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
UIImage *gradientImage32 = [[UIImage imageNamed:#"header_Blue.png"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
// Set the background image for *all* UINavigationBars
[[UINavigationBar appearance] setBackgroundImage:gradientImage44
forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:gradientImage32
forBarMetrics:UIBarMetricsLandscapePhone];
//[[UINavigationBar appearance]setBackgroundColor:[UIColor blueColor]];
[self.view setNeedsDisplay];
}
Now my problem is that the navigation bar changes take effect when i go to another view.but it does not change immediately after the button click for same view controller.
I know the reason that my navigation bar drawn already so it cant change on button click..so can anyone please tell me how can i change my current view's Navigation bar even after a button click..
May be i have to reload the whole view.. but don't know how can i do it.
Thanks in Advance.
Call this method on your UIButton Action method:
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed: #"header_Blue.png"]
forBarMetrics:UIBarMetricsDefault];
I have set a custom navigationbar in my appdelegate. You can see the code over here.
UIImage *image = [UIImage imageNamed:#"navbar.png"];
[[UINavigationBar appearance] setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
But now I'm working with the eventkit framework. What I want is when I go to eventdetails, that I get the standard navbar layout.So without the image.
EKEventViewController *vc = [[EKEventViewController alloc] init];
[vc.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
vc.event = [dataSource eventAtIndexPath:indexPath];
vc.allowsEditing = YES;
[calendar.navigationController pushViewController:vc animated:YES];
I've tried the following but it is not working.
Any help?
you can do one thing, take the screenshot of one viewController with default navigation bar , just crop only navigation bar area i.e. make image of 320 x 44 size.
when you want again your by default navigation bar , that time use this cropped image as background of navigation bar ,add following code
UIImage *image = [UIImage imageNamed:#"defaultNavbar.png"];
[[UINavigationBar appearance] setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
again when you navigate to another viewController having custom Navimage then again draw nav image by help of your custom image code i.e.
UIImage *image = [UIImage imageNamed:#"navbar.png"];
[[UINavigationBar appearance] setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
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.
I have made an iPhone application in Xcode 4.2 and snow leopard in which I have set the navigation bar and tab bar background color by implementing following code
CGRect frame = CGRectMake(0.0, 0.0, self.view.bounds.size.width, 48);
UIView *v = [[UIView alloc] initWithFrame:frame];
[v setBackgroundColor:[UIColor colorWithRed:1.0f/255.0f green:140.0f/255.0f
blue:131.0f/255.0f alpha:1.0f]];
[[self.tabBarController tabBar] insertSubview:v atIndex:0];
[v release];
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:1.0f/255.0f
green:125.0f/255.0f blue:131.0f/255.0f alpha:1.0f];
it is working fine but now if I am running this app in ios 5 and xcode 4.2 , navigation bar color set but tabbar background color doesn't set
How can I set background color of tabbar in ios 5? If someone know please help me.
Thanks alot.
CGRect frame = CGRectMake(0.0, 0.0, 320, 48);//Setting a Frame.
myTabView = [[UIView alloc] initWithFrame:frame];//Making Tab View
// not supported on iOS4
UITabBar *tabBarr = [self.tabBar tabBar];
if ([tabBarr respondsToSelector:#selector(setBackgroundImage:)])
{
// set it just for this instance
[tabBarr setBackgroundImage:[UIImage imageNamed:#"hot-1.png"]];
// set for all
// [[UITabBar appearance] setBackgroundImage: ...
}
else
{
// ios 4 code here
//[tabBarr setBackgroundColor:c];
}
//[myTabView setBackgroundColor:c];//Setting Color Of TaBar.
[myTabView setAlpha:0.8];//Setting Alpha of TabView.
[[self.tabBar tabBar] insertSubview:myTabView atIndex:0];//Inserting Tab As SubView.
Use this instead of setting color to background of tabbar ....
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]];
}