覆盖UINavigationBar以设置默认titleView [英] Overwriting UINavigationBar to set a default titleView

查看:392
本文介绍了覆盖UINavigationBar以设置默认titleView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是一个带有图像的导航栏。我的主视图上有一个标签控制器,在每个标签内我都有一个UINavigationController。从我的tab / navigationController调用的UIViewController内部,我可以设置titleView没有太大问题,在viewDidLoad方法中执行此操作:

What I want to do is a navigation bar with a image on it. I have a tab controller on my main view, and inside each tab I have a UINavigationController. From inside the UIViewController that my tab/navigationController calls, I could set the titleView without much problem, doing this inside the viewDidLoad method:

self.navigationItem.titleView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mylogo.png"]] autorelease];

但是,我想在此视图中替换导航栏中的所有标题,重复它似乎很难看这无处不在。所以我在委托上做了这个(在链接所有Outlet东西之后)

But, I want to replace all titles in my navigationBar for this view, and it seems ugly to repeat this everywhere. So I did this on the delegate (after linking all the Outlet stuff)

self.tabOneNavController.navigationBar.topItem.titleView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mylogo.png"]] autorelease];

再次,它有效!好的,我快到了那里。
但关键是,我有5个标签,所有这些都有内置的navigationControllers。我将代码重复从每个内部视图减少到只有5次,但它仍然存在。它要求我为每个选项卡的NavController执行此操作。

Again, it worked! ok, I'm almost getting there. But the point is, I've 5 tabs and all of them have navigationControllers inside. I reduced the code repetition from every internal view to only 5 times, but it still. It requires that I do that for the NavController of each tab.

然后我尝试扩展UINavigationBar以创建我自己的,我可以在初始化程序中设置它,并且在接口构建器中将它用作对象类。但它似乎没有用。这是我做的:

Then I tried to extend the UINavigationBar to create my own, where I could set this in the initializer, and use it in the interface builder as the object class. But it doesn't seem to work. Here is what I did:

@implementation MyNavigationBar

- (id)init {
    self = [super self];

    self.tintColor = [UIColor greenColor];
    self.topItem.title = @"testing please work";

    return self;
}

@end

接口文件中的

MyNavigationBar继承来自UINavigationBar。但这没效果。我应该覆盖其他方法吗?哪一个?这是一个好习惯吗?
我甚至不确定是否应该为每个标签添加一个navigationBar,正如我所说的,我有标签,我想要一个导航栏/在其中导航。到目前为止,经过近乎死亡的经验,试图弄清楚界面构建器/出口和类是如何工作的,代码正在运行,我只是想让它成为unglify。

in the interface file MyNavigationBar inherits from UINavigationBar. But this didn't work. Should I overwrite other method? which one? is this a good practice? I'm not even sure if I should add one navigationBar for each tab, as I said, I have tabs and I want to have a navigation bar / navigate inside them. By now, after a near death experience trying to figure out how the interface builder / outlets and classes work, the code is working, I just would like to make unglify it.

谢谢!

推荐答案

重复您所描述的代码的问题有一个优雅的解决方案。 Objective-C支持称为类别的东西,它允许您向类添加方法。常见的用途是自定义导航和标签栏。在Xcode 4中,您可以执行以下操作在UINavigationBar上添加类别:

The problem of repeating code which you describe has an elegant solution. Objective-C supports something called a "category", which allows you to add methods to a class. A common use for this is to customize navigation and tab bars. In Xcode 4, you would do something like this to add a category on UINavigationBar:

按Command + N或打开新建文件对话框。接下来,从Cocoa Touch菜单中选择Objective-C category:

Hit Command+N or open the "New File" dialog. Next, choose "Objective-C category" from the Cocoa Touch menu:

单击下一步,系统将提示您输入要作为类别添加方法的类的名称。看起来应该是这样的:

Click Next and you will be prompted to enter the name of the class that you would like to add methods to as a category. It should look something like this:

然后,您应该最终得到一个保存文件对话框。这里有关于约定的快速说明。惯例是在原始类之后命名一个类别,加号,然后描述您要添加的内容。以下是您的看法:

Then, you should end up with a save file dialog. A quick note about convention here. Convention is to name a category after the original class, the plus sign, and then a description of what you're adding. Here's what yours might look like:

保存文件后,您需要得到以下内容:

Once you save your file, you will need get something like this:

看看那美丽。您现在可以覆盖默认的绘图/初始化方法以及扩展导航栏的功能。

Look at that beauty. You can now override the default drawing/init methods as well as extend the functionality of the navbar.

我建议查看 init drawRect 方法,虽然我不记得人们使用哪些方法。此外,请注意,虽然在NDA下,这可能会在iOS 5中发生变化,因此请为此做好准备。

I'd suggest looking into the init and drawRect methods, although I don't remember which ones people use. Also, please note that while under NDA, this may change in iOS 5, so just be prepared for that possibility.

这篇关于覆盖UINavigationBar以设置默认titleView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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