子类化UINavigationBar ...如何在UINavigationController中使用它? [英] Subclassing UINavigationBar ... how do I use it in UINavigationController?

查看:123
本文介绍了子类化UINavigationBar ...如何在UINavigationController中使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

@property(nonatomic, readonly) UINavigationBar *navigationBar

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

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

这是我的UINavigationBar子类:

Here is my UINavigationBar subclass:

#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

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

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

任何建议或其他解决方案?我基本上想要创建一个浅色背景和黑暗文本,如Flickr的应用程序导航栏

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

推荐答案

我不知道如果你弄明白的话。但是对于可能有这个问题的其他人......

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

你需要做的是将你的XIB中的类指定为你的类名(在这种情况下是MyNavigationBar。

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

检查WWDC 2011中的会话123.

Check session 123 in WWDC 2011.

这篇关于子类化UINavigationBar ...如何在UINavigationController中使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆