覆盖 UINavigationBar 以设置默认的 titleView

发布于 2024-11-27 16:45:37 字数 1218 浏览 0 评论 0原文

我想做的是一个带有图像的导航栏。我的主视图上有一个选项卡控制器,每个选项卡内都有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羁〃客ぐ 2024-12-04 16:45:37

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

点击 Command+N 或打开“新建文件”对话框。接下来,从 Cocoa Touch 菜单中选择“Objective-C 类别”:

创建类别

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

在导航栏上创建类别

然后,您最终应该看到一个保存文件对话框。这里有一个关于约定的快速说明。惯例是按照原始类、加号以及要添加的内容的描述来命名类别。您的文件可能如下所示:

Category Naming Convention

保存文件后,您将需要获得如下内容:

空类别

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

我建议查看 initdrawRect 方法,尽管我不记得人们使用了哪些方法。另外,请注意,在 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:

Creating a category

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:

Making a Category on the NavBar

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:

Category Naming Convention

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

Empty Category

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 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.

心房的律动 2024-12-04 16:45:37

为什么不定义一个 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文