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.
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 add UINavigationBar via Library to view. I also add UINavigationItem to this NavigationBar.
In viewController.m,I added the following code but back button doesn't show.
self.navigationItem.title = #"List";
self.navigationItem.backBarButtonItem.title = #"Back";
Try this:
self.navigationItem.leftItemsSupplementBackButton = YES;
or
navigationItem.leftItemsSupplementBackButton = true
If you customize your navigation controller with UIBarButtonItems the framework removes the back button by default, since it assumes you are customizing the Navigation bar. Use that line to add the back button in again.
self.navigationItem.backBarButtonItem will be shown only after your have pushed another one view to navigation stack, controlled by self.navigationController, if no left button on navigation bar is displayed.
From Apple docs:
When this item is the back item of the navigation bar—when it is the next item below the top item—it may be represented as a back button on the navigation bar. Use this property to specify the back button. The target and action of the back bar button item you set should be nil. The default value is a bar button item displaying the navigation item’s title.
Try this code,
**To hide your default back button use this,**
self.navigationItem.hidesBackButton = YES;
UIImage* image = [UIImage imageNamed:#"back.png"];
CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
UIButton* backbtn = [[UIButton alloc] initWithFrame:frame];
[backbtn setBackgroundImage:image forState:UIControlStateNormal];
[backbtn setShowsTouchWhenHighlighted:YES];
[backbtn addTarget:self action:#selector(goBack) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem* backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backbtn];
[self.navigationItem setLeftBarButtonItem:backButtonItem];
[backButtonItem release];
[backbtn release];
Action Event for back button:
-(IBAction)goBack{
//ur code here
}
Try this code may be it's help to u
UIButton *moreButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
[moreButton1 setImage:[UIImage imageNamed:#"left_arrow.png"] forState:UIControlStateNormal];
//[moreButton setImage:[UIImage imageNamed:#"Button_OtherInfo_Active.png"] forState:UIControlStateHighlighted];
[moreButton1 addTarget:self action:#selector(backClick) forControlEvents:UIControlEventTouchUpInside];
[moreButton1 setFrame:CGRectMake(0, 0, 50,30)];
self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:moreButton1];
put this code in viewDidLoad
-(void)backClick
{
[self.navigationController popViewControllerAnimated:YES];
}
This is a bit old, but in case someone is looking for an answer...
The back button shows up automatically, but the current view (not the one being pushed) needs to have a title. eg:
- (void)viewDidLoad
{
self.title = #"Home";
[super viewDidLoad];
}
Then, when you push a view onto the view stack with
[self.navigationController pushViewController:aViewController animated:YES];
the back button shows up. No need to mess with UINavigationItem or UINavigationBar. Use those to customize the navigation bar. Take a look at the example project called NavBar, part of xcode.
I have a custom UINavigationBar that has a different image background and height than the default one. It's displayed normally but as soon as I go back and forth between apps the background image and buttons inside the navigation bar jump up.
The UINavigationBar is created in a nib and has a custom class that overrides the default implementation to add the image:
#implementation MyUINavigationBar
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: #"nav.png"];
rect.size.height = 60;
[image drawInRect:rect];
}
#end
At this point the bar isn't displayed correctly. In - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions of my app delegate I fix this with this code:
self.navigationController.view.frame = CGRectMake(0, 20, 320, 460);
self.navigationController.navigationBar.frame = CGRectMake(0, 0, 320, 60);
Now everything shows up correctly. When I switch to another app and go back again the bar jumps up, just as if the frames weren't applied. This happens right after I switch back to the app.
Below are two screenshots showing what's going on. The first one shows the correct version, the other one the faulty one.
Any idea what might be going on? I tried applying the new frames in viewWillAppear and viewDidAppear of the active view or in applicationDidBecomeActive of the app delegate but that didn't help.
https://skitch.com/instromaniac/rtagr/ios-simulator
https://skitch.com/instromaniac/rtaf9/view-not-ok
I spent days to figure this out, here is the trick that really work for a complete, customized backButton.
The backBarButtonItem is totally locked down in the iOS SDK (4.3 as far as I know), so we have to use a leftBarButtonItem that do the job in place of the regular back button.
Whereas the backBarButtonItem has to be defined in the Root View Controller of your UINavigationController, we have to setup our fake BackButton in the child view (ie. nextViewController in my example).
This code is in my Root View Controller (Which is a UITableViewDelegate), in -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
[[self navigationController] pushViewController:nextViewController animated:YES];
here is the graal for displaying our fake back button in a 70px Custom UINavigationBar (Play with the frames to fit your needs) :
-(void)handleBack:(id)sender {
// Simulate the original back button
[self.navigationController popViewControllerAnimated:YES];
}
-(void)viewWillAppear:(BOOL)animated {
// Making a custom button
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[backBtn setImage:[UIImage imageNamed:#"back-button.png"] forState:UIControlStateNormal];
[backBtn setImage:[UIImage imageNamed:#"back-button-hover.png"] forState:UIControlStateHighlighted];
[backBtn setImage:[UIImage imageNamed:#"back-button-disabled.png"] forState:UIControlStateDisabled];
[backBtn setFrame:CGRectMake(6, 4, 60, 30)];
// Binding custom target & action to the button
[backBtn addTarget:self action:#selector(handleBack:) forControlEvents:UIControlEventTouchUpInside];
// Nesting the button in a UIView to position the button anywhere!
UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 70)];
[backView addSubview:backBtn];
// Nesting all of these into a UIBarButtonItem
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backView];
// Overwrite original backbutton (self.navigationItem.backBarButtonItem DO NOT WORK HERE
self.navigationItem.leftBarButtonItem = backItem;
// Memory clean up
[backItem release];
[backView release];
}
Have fun ;)
Try changing the frame of the navigation controller's view as
self.navigationController.view.frame = CGRectMake(0,0,320,480);
Hope this helps
How can the UINavigationBar customizations be done? Is it using subclassing or categories?
I am interested in 2 aspects:
Adding an image to the NavBar (like the FOXNEWS logo)
Customizing the back button to "Shows". (the back button usually takes the title of the previous view in the stack, but there is no title in previous view.)
Thanks in advance for any help
For the Fox News app it looks like they just set the tint color of the navigation bar. As for the Fox News logo, it's probably just an image view on the title view of the navigation bar. This code goes into a view controller's viewDidLoad method:
[self.navigationController.navigationBar setTintColor:/* Custom color here */];
UIImageView *logoView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"logo"]];
[self.navigationItem setTitleView:logoView];
[logoView release];
To customize the back button you need to place this in the viewDidLoad method of the previous view controller (i.e. the one that this button leads back to):
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Shows"
style:UIBarButtonItemStyleBordered target:nil action:nil];
[self.navigationItem setBackBarButtonItem:backButton];
[backButton release];
If you want to use a totally custom background image for your application's navigation bar, you need to create a custom UINavigationBar category and draw the image within its drawRect: method. Something like this:
#implementation UINavigationBar (UINavigationBarBackgroundImage)
- (void)drawRect:(CGRect)rect
{
[[UIImage imageNamed:#"navigation-bar"] drawInRect:rect];
// Optionally you can set the tintColor here to go with the background
}
#end
adding image to navigation bar use this code
- (void)drawRect:(CGRect)rect {
UIColor *color = [UIColor blackColor]; //this use to set button color in FoxNew Site //button color
UIImage *img = [UIImage imageNamed: #"ImageName"];
[img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
self.tintColor = color;
}
create category for UINavigationBar
#implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
}
#end
In our application I use Left and right button on navigation bar.
I want a image on back (left)button.If i use segment for that then
necessary to define a action for back button.
Please advice me for any method.
Yeah, if you use a Bar Button item you can go to Bar Button Item Attributes in Interface Builder and there should an Image option you can select.
The easiest way we found to replace the back button with our own goes like this.
Add a back button in the nib file outside your view. customize it as you wish.
connect it as an IBOutlet. Let's call it btnBack.
in ViewDidLoad do this:
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithCustomView:btnBack];
[self.navigationItem setLeftBarButtonItem:back animated:YES];
connect an action to your backBtn like this:
- (IBAction)Back:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
}
And thats it you got yourself a customized back button :)
You can do it by subclassing UINavigationBar, then create a method like this :
+ (UIBarButtonItem *)barButtonWithImage:(NSString *)imageName target:(id)target action:(SEL)action {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
UIImage *titleImage = [UIImage imageNamed:imageName];
[button setImage:titleImage forState:UIControlStateNormal];
[button sizeToFit];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
return item;
}
And then add it to your navigation bar like this :
[self.navigationItem setLeftBarButtonItem:[TFNavigationBar barButtonWithImage:#"rp_settings.png" target:self action:#selector(showSettings)]];
p.s: I prefer to use singletons, that's why that it is.
If you want to simply replace your back button with an image through out your app..
[[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:#"back_button"]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:#"back_button"]];
Or for specific navigation bars specify your navBar instead of '[UINavigationBar appearance]'. This works perfectly.