8

UINavigationBar (カスタムの背景画像とテキストの色を設定するため) をサブクラス化し、それをアプリのすべてのナビゲーション バーに使用したいと考えました。UINavigationController の API ドキュメントを見ると、navigationBar は読み取り専用のようです。

@property(nonatomic, readonly) UINavigationBar *navigationBar

Is there a way to actually use a custom UINavigationBar in my UIViewControllers? I know that other apps have done custom navigation bars, like flickr:

http://img.skitch.com/20100520-bpxjaca11h5xne5yakjdc2m6xx.png

Here is my UINavigationBar subclass:

#import <UIKit/UIKit.h>

@interface MyNavigationBar : UINavigationBar <UINavigationBarDelegate> {

}

@end

the implementation

#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 know that I can use a category to change the background image, but i still want to be able to set the text color of the navigation bar title

@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

any suggestions or other solutions? I basically want to create a light background and dark text like Flickr's app navigation bars

4

3 に答える 3

4

あなたがこれを理解したかどうかはわかりません。しかし、この問題を抱えている可能性のある他の人にとっては...

必要なことは、XIB のクラスをクラス名に指定することです (この場合は MyNavigationBar.

WWDC 2011 のセッション 123 を確認してください。

于 2011-09-23T20:24:36.170 に答える
2

UINavigationBarの「tint」プロパティを希望の色に設定します。

于 2010-05-21T09:32:43.843 に答える
2

を作成し[UIColor colorWithPatternImage:(UIImage*)image]て、NavBar の backgroundColor プロパティに設定してみてください。私はまだこれを試していませんが、今すぐやります。

于 2010-05-21T23:32:58.370 に答える