覆盖 UINavigationBar 以设置默认的 titleView
我想做的是一个带有图像的导航栏。我的主视图上有一个选项卡控制器,每个选项卡内都有一个 UINavigationController。从我的 tab/navigationController 调用的 UIViewController 内部,我可以毫无问题地设置 titleView,在 viewDidLoad 方法内执行此操作:
self.navigationItem.titleView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mylogo.png"]] autorelease];
但是,我想替换此视图的 navigationBar 中的所有标题,并且在各处重复此操作似乎很难看。所以我在委托上执行了此操作(在链接所有 Outlet 内容之后)
self.tabOneNavController.navigationBar.topItem.titleView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mylogo.png"]] autorelease];
,它再次起作用了!好的,我快到了。 但重点是,我有 5 个选项卡,并且所有选项卡内部都有 navigationController。我将每个内部视图的代码重复减少到只有 5 次,但仍然如此。它要求我为每个选项卡的 NavController 执行此操作。
然后我尝试扩展 UINavigationBar 来创建我自己的,我可以在初始化程序中设置它,并在界面生成器中将其用作对象类。但这似乎不起作用。这就是我所做的:
@implementation MyNavigationBar
- (id)init {
self = [super self];
self.tintColor = [UIColor greenColor];
self.topItem.title = @"testing please work";
return self;
}
@end
在界面文件中,MyNavigationBar 继承自 UINavigationBar。但这没有用。我应该覆盖其他方法吗?哪一个?这是一个好的做法吗? 我什至不确定是否应该为每个选项卡添加一个导航栏,正如我所说,我有选项卡,并且我想要一个导航栏/在其中导航。到目前为止,在尝试弄清楚界面生成器/插座和类如何工作的近乎死亡的经历之后,代码正在工作,我只想将其取消。
谢谢你!
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];
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];
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.
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
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.
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您描述的重复代码的问题有一个优雅的解决方案。 Objective-C 支持称为“类别”的东西,它允许您向类添加方法。其常见用途是自定义导航和选项卡栏。在 Xcode 4 中,您可以执行类似的操作在 UINavigationBar 上添加类别:
点击 Command+N 或打开“新建文件”对话框。接下来,从 Cocoa Touch 菜单中选择“Objective-C 类别”:
单击“下一步”,系统将提示您输入您想要添加方法作为类别的类的名称。它应该看起来像这样:
然后,您最终应该看到一个保存文件对话框。这里有一个关于约定的快速说明。惯例是按照原始类、加号以及要添加的内容的描述来命名类别。您的文件可能如下所示:
保存文件后,您将需要获得如下内容:
看看那美丽的景色。您现在可以覆盖默认的绘图/初始化方法以及扩展导航栏的功能。
我建议查看
init
和drawRect
方法,尽管我不记得人们使用了哪些方法。另外,请注意,在 NDA 下,这可能会在 iOS 5 中发生变化,因此请为这种可能性做好准备。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:
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.
I'd suggest looking into the
init
anddrawRect
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.为什么不定义一个 UIViewController 子类,通过 self.navigationItem.titleView 设置标题视图,并让其他视图控制器从该类扩展?然后,您可以在所有控制器之间共享该行为,而无需重复实施。
Why not define a UIViewController subclass which sets the title view via
self.navigationItem.titleView
and have your other view controllers extend from that class? Then you're sharing that behavior across all of your controllers without repeating the implementation.