本文介绍了覆盖 navigationController 的 navigationBar 属性的最佳点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在覆盖 UINavigationController 以使用我自己的 UINavigationBar 子类的实例替换默认的 navigationBar 属性.所以我尝试了类似

I'm overwriting UINavigationController to replace the default navigationBar property with an instance of my own subclass of UINavigationBar. So I tried something like

_navigationBar = [[SBNavigationBar alloc] init];

在我的 -initWithRootViewController: 中.但这并没有像我预期的那样成功.仍然显示默认的 navigationBar.

in my -initWithRootViewController:. But that didn't work out as I expected it. There's still the default navigationBar being displayed.

那么覆盖 navigationBar 的最佳点是什么?

So what's the best point to overwrite the navigationBar?

提前致谢
-f

推荐答案

如果你查看文档 http://developer.apple.com/iphone/library/documentation/uikit/reference/UINavigationController_Class/Reference/Reference.html,你会看到navigationBar 属性是只读.

If you check the documentation http://developer.apple.com/iphone/library/documentation/uikit/reference/UINavigationController_Class/Reference/Reference.html, you'll see that the navigationBar property is read-only.

要自定义 navigationBar,您可以使用 Categories.您会在 stackoverflow 上找到许多问题的答案.

To have a custom navigationBar you can use Categories for example. You will find many questions answering this here on stackoverflow.

一个简单的版本是将图像放入 drawRect: 就像这样......

One simple version is putting an image in drawRect: like so...

@implementation UINavigationBar (Custom)

- (void)drawRect:(CGRect)rect {
    UIImage *image = [UIImage imageNamed: @"bg_toolbar.png"];
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}

@end

这篇关于覆盖 navigationController 的 navigationBar 属性的最佳点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 06:04