iPhone custom UINavigationBar buttons
I have an application with 4 开发者_高级运维tabs. Each tab is a UINavigationController. The 4 UINavigationBar tabs should look the same, have a custom background image, a custom backButton and a custom right button triggering a function.
I would like to do those customizations only once in my code and not in each RootViewController.
I managed to have a custom background image by inserting this code into my appDelegate:
@implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: @"MyNavigationBar.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end
But I didn't manage to customize the back and right buttons or specify the action for the right button.
Is there a way to do that in the appDelegate, just like for the background image?
Or should I do the customization in each RootViewController?Write the below code in viewWillAppear
method
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *butImage = [[UIImage imageNamed:@"back.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:10];
[button setBackgroundImage:butImage forState:UIControlStateNormal];
[button addTarget:self action:@selector(gotoBack:) forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(0, 0, 48, 30);
UIBarButtonItem *backButton = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
self.navigationItem.leftBarButtonItem = backButton;
and write the action event for backButton.
-(IBAction)gotoBack:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
In your appdelegate in aplicationDidFinishLaunching.....
UIImage *navBG = [UIImage imageNamed:@"barra-logo-centro.png"];
[[UINavigationBar appearance] setBackgroundImage:navBG forBarMetrics:UIBarMetricsDefault];
//backbutton: 22x30 , 44x60@2x
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[UIImage imageNamed:@"back_button.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackgroundImage:[UIImage imageNamed:@"normal_button.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
this will modify the complete proyect navigationbar and bar button ítems
Renungas answer works fine.
If you don't want to write the same code 4 times you can always subclass UINavigationController.
I just tried this solution (with subclassing UITabBarController but nevertheless...) and it works ok.
You can find similiar example here
Your custom code (identical to the mentioned example) should look like this:
- (void)loadView {
[super loadView];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"backbutton.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 32, 32)];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
}
- (void)backAction {
[self.navigationController popViewControllerAnimated:YES];
}
As you can see all you have to do is override loadView and add a method to perform the popVievController selector.
Good luck!
精彩评论