子类化 UINavigationBar ...如何在 UINavigationController 中使用它?

发布于 2024-09-02 01:16:29 字数 2557 浏览 8 评论 0原文

我想对 UINavigationBar 进行子类化(以设置自定义背景图像和文本颜色)并将其用于我的应用程序中的所有导航栏。查看UINavigationController的API文档,看起来navigationBar是只读的:

@property(非原子,只读)UINavigationBar *navigationBar

有没有办法在我的 UIViewControllers 中实际使用自定义 UINavigationBar ?我知道其他应用程序已经做了自定义导航栏,例如 flickr:

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

这是我的 UINavigationBar 子类:

#import <UIKit/UIKit.h>

@interface MyNavigationBar : UINavigationBar <UINavigationBarDelegate> {

}

@end

实现

#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

我的 知道我可以使用类别来更改背景图像,但我仍然希望能够设置导航栏标题的文本颜色

@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

有什么建议或其他解决方案吗?我基本上想创建一个浅色背景和深色文本,就像 Flickr 的应用程序导航栏一样

I wanted to subclass UINavigationBar (to set a custom background image & text color) and use that for all the navigation bars in my app. Looking at the API docs for UINavigationController, it looks like navigationBar is read-only:

@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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

漫雪独思 2024-09-09 01:16:29

我不知道你是否明白了这一点。但对于其他可能遇到此问题的人...

您需要做的是将 XIB 中的类指定为您的类名(在本例中为 MyNavigationBar。

检查 WWDC 2011 中的会话 123。

I dunno if you got this figured out. But for others that might have this problem...

What you need to do is to specify the class in your XIB to your class name (in this case MyNavigationBar.

Check session 123 in WWDC 2011.

携君以终年 2024-09-09 01:16:29

将 UINavigationBar“tint”属性设置为您想要的颜色。

Set the UINavigationBar "tint" property to the color you want.

画尸师 2024-09-09 01:16:29

尝试创建一个[UIColor colorWithPatternImage:(UIImage*)image]并将其设置为导航栏的backgroundColor属性。我还没有尝试过,但我现在就会这样做。

Try creating a [UIColor colorWithPatternImage:(UIImage*)image] and setting it to the NavBar's backgroundColor property. I haven't tried this yet, but I will do it right now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文