Je voulais sous-classer UINavigationBar (pour définir une image de fond personnalisée & couleur de texte) et l'utiliser pour toutes les barres de navigation dans mon application. En regardant l'API docs pour UINavigationController, il ressemble à navigationBar est en lecture seule:Sous-classe UINavigationBar ... comment l'utiliser dans UINavigationController?
@property (nonatomic, en lecture seule) UINavigationBar * navigationBar
Est-il possible d'utiliser effectivement un UINavigationBar personnalisé dans mes UIViewControllers? Je sais que d'autres applications ont fait des barres de navigation personnalisés, comme flickr:
http://img.skitch.com/20100520-bpxjaca11h5xne5yakjdc2m6xx.png
Voici ma sous-classe UINavigationBar:
#import <UIKit/UIKit.h>
@interface MyNavigationBar : UINavigationBar <UINavigationBarDelegate> {
}
@end
la mise en œuvre
#import "MyNavigationBar.h"
@implementation MyNavigationBar
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Initialization code
}
return self;
}
- (void)drawRect:(CGRect)rect {
// override the standard background with our own custom one
UIImage *image = [[UIImage imageNamed:@"navigation_bar_bgd.png"] retain];
[image drawInRect:rect];
[image release];
}
#pragma mark -
#pragma mark UINavigationDelegate Methods
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
// use the title of the passed in view controller
NSString *title = [viewController title];
// create our own UILabel with custom color, text, etc
UILabel *titleView = [[UILabel alloc] init];
[titleView setFont:[UIFont boldSystemFontOfSize:18]];
[titleView setTextColor:[UIColor blackColor]];
titleView.text = title;
titleView.backgroundColor = [UIColor clearColor];
[titleView sizeToFit];
viewController.navigationItem.titleView = titleView;
[titleView release];
viewController.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.1 green:0.2 blue:0.3 alpha:0.8];
}
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
}
- (void)dealloc {
[super dealloc];
}
@end
I sachez que je peux utiliser une catégorie pour changer l'arrière-plan image, mais je veux toujours être en mesure de définir la couleur du texte du titre de la barre de navigation
@implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: @"navigation_bar_bgd.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end
des suggestions ou d'autres solutions? Je souhaite fondamentalement créer un arrière-plan clair et un texte sombre comme les barres de navigation de l'application Flickr
Cela fonctionnerait pour changer l'arrière-plan de la barre de navigation, mais je voudrais aussi changer la couleur du texte blanc par défaut de la barre de navigation, puisque je veux utiliser une couleur de fond de couleur claire, par exemple si je voulais utiliser fond blanc, je devrais être en mesure de changer le texte blanc par défaut à quelque chose de sombre – funkadelic