【问题标题】:Subclassing UINavigationBar ... how do I use it in UINavigationController?子类化 UINavigationBar ...如何在 UINavigationController 中使用它?
【发布时间】:2011-02-22 03:33:14
【问题描述】:

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

@property(nonatomic, readonly) 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 的应用导航栏这样的浅色背景和深色文本

【问题讨论】:

    标签: iphone uinavigationcontroller uinavigationbar subclassing


    【解决方案1】:

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

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

    查看 WWDC 2011 中的会话 123。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 这可以更改导航栏的背景,但我也想更改导航栏的默认白色文本颜色,因为我想使用浅色背景颜色。例如。如果我想使用白色背景,我需要能够将默认的白色文本更改为深色
      【解决方案3】:

      尝试创建一个[UIColor colorWithPatternImage:(UIImage*)image] 并将其设置为导航栏的背景颜色属性。我还没有尝试过,但我会立即尝试。

      【讨论】:

        猜你喜欢
        • 2017-11-02
        • 1970-01-01
        • 2011-10-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-15
        • 2010-12-24
        相关资源
        最近更新 更多