これはシモーネの回答と同じですが、iOS 5 および iOS < 5 で機能します。これは私がアプリで使用しているものです。アプリの初期化のどこかで呼び出す必要があり[UINavigationBar setupIos5PlusNavBarImage]
ます (applicationDidFinishLaunching: が適切な候補です)。iOS 5 以降では、setupIos5PlusNavBarImage は新しい UIAppearance プロトコルを使用して背景を設定し、drawRect オーバーライドは無視されます。iOS < 5 では、setupIos5PlusNavBarImage は基本的にノーオペレーションであり、drawRect が画像の描画を処理します。
インターフェース:
@interface UINavigationBar (CustomNavigationBar)
+ (void) setupIos5PlusNavBarImage;
- (void) drawRect: (CGRect) rect;
@end
実装:
@implementation UINavigationBar (CustomNavigationBar)
+ (void) setupIos5PlusNavBarImage
{
if ([UINavigationBar respondsToSelector: @selector(appearance)])
{
[[UINavigationBar appearance] setBackgroundImage: [UIImage imageNamed: @"menuBar.png"] forBarMetrics: UIBarMetricsDefault];
}
}
- (void) drawRect: (CGRect) rect
{
UIImage* img = [UIImage imageNamed: @"menuBar.png"];
[img drawInRect: CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end