自定义导航栏 [英] Custom navigation bar
问题描述
我正在抓住Dribble并找到了附件设计。我想知道如何做这样的自定义导航栏。我的意思是如何创建一次导航栏并为每个视图控制器隐式重用它。
I was crawling Dribble and found the attached design. I was wondering how to do a custom navigation bar like this. I mean how create the navigation bar once and reuse it implicitly for every view controllers.
我正在考虑拥有一种根视图控制器并继承其他视图控制器,但是我不知道怎么做。
I was thinking about having a kind of root view controller and inherit for other view controllers, but I don't know how to do this.
任何想法或链接都会被公布!
Any idea or link will be appreachated!
干杯。
Cyril
推荐答案
感谢iOS5,您现在可以自定义UINavigationBar的外观无需子类或创建类别。
Thanks to iOS5 you are now able to customise the appearance of a UINavigationBar without having to subclass or create a category.
以下代码块(将其放在applicationDidFinishLoading:方法中)将整个应用程序的UINavigationBar更改为您提供的任何图像它。
The following code block (put it in your applicationDidFinishLoading: method) will change the UINavigationBar for the whole application to whatever image you give it.
注意,这只适用于iOS5
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@nav bar.png] forBarMetrics:UIBarMetri csDefault];
但是,您还可以根据您使用的视图控制器来更改单个UINavigationBar的外观viewDidLoad中的以下代码。
However, you are also able to change the appearance of a single UINavigationBar depending on what view controller you're in by using the following code in viewDidLoad.
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@nav bar.png] forBarMetrics:UIBarMetricsDefault ];
以上代码仅讨论了iOS5自定义UINavigationBar外观的新方法。但是,它没有讨论按钮的实现方式。
然而,添加按钮完全是一个不同的游戏。为此,我建议继承 UINavigationBar
,然后在需要的地方添加按钮。您甚至可能只使用标准的 UINavigationBar
,但自定义 UIBarButtonItem
以特定视图运行。
However, adding the buttons is a different game altogether. For this, I would recommend subclassing UINavigationBar
, and then adding in the buttons where needed through that. You could probably even get away with just a standard UINavigationBar
but custom UIBarButtonItem
s that run off a particular view.
例如:
UIView *rightButton = [[[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 30.0f, 30.0f)] autorelease];
[rightButton addSubview:[UIImage imageNamed:@"rightButtonImage.png"]];
UIBarButtonItem *rightButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:rightButton] autorelease];
[rightButtonItem setAction:@selector(rightButtonAction:)];
我没有测试过该代码,因此它不是复制/粘贴解决方案,但它给出了你知道需要做些什么来完成自定义看UIBarButtonItems。
I haven't tested that code so it isn't a copy/paste solution, but it gives you an idea of what needs to be done to accomplish "custom" looking UIBarButtonItems.
祝你好运!
这篇关于自定义导航栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!