【问题标题】:How do I customize the navigation bar on the iPhone in Xcode ?如何在 Xcode 中自定义 iPhone 上的导航栏?
【发布时间】:2012-01-07 16:12:46
【问题描述】:

我有以下代码-sn-ps:

1.)

[[UINavigationBar appearance] setBackgroundImage:myImage forBarMetrics:UIBarMetricsDefault];

我会把它放在哪里?

或 2.)

@implementation UINavigationBar (BackgroundImage)
//This overridden implementation will patch up the NavBar with a custom Image instead of the title
- (void)drawRect:(CGRect)rect {
     UIImage *image = [UIImage imageNamed: @"NavigationBar.png"];
     [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end

我会把它放在哪里?

我已经尝试在我的视图控制器中添加一个导航栏,但我不知道将这些代码放在哪里。

【问题讨论】:

  • 您还可以询问您需要自定义什么,例如更改整个导航颜色/bar/titleImage...等。
  • 问题似乎是重复的,您可以参考以下线程 - stackoverflow.com/questions/704558/…

标签: iphone objective-c xcode uinavigationbar


【解决方案1】:

片段 1 仅适用于 iOS 5.0。您可能会在创建导航栏之前将其放在applicationDidFinishLaunching: 中。在这里查看更多信息:

Snippet 2 是您在 iOS 5.0 之前必须要做的。这是UINavigationBar 上的一个类别。我之前实际上没有在一个类别中看到覆盖drawRect:,但它似乎确实有效。只需在 UINavigationBar 上的项目中创建一个类别并添加该代码。所以像:

UINavigationBar+MyCategory.h:

@interface UINavigationBar (MyCategory)
- (void)drawRect:(CGRect)rect;
@end

UINavigationBar+MyCategory.m:

#import "UINavigationBar+MyCategory.h"

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

【讨论】:

  • 无需将drawRect 放入.h 文件中。它不是公开的,不应直接调用。
  • 我对objective-c很陌生,不幸的是我碰巧从未使用过一个类别......所以基本上我必须创建一个名为UINavigationBar+MyCategory.h的新类UINavigationBar?
  • 我建议你去阅读一下什么是 Objective-C 类别(尝试 Wikipedia 或 Cocoa Dev Central),然后它就会变得清晰。我展示的是要创建的 2 个文件,然后将它们添加到您的项目中。
  • 好的,谢谢。如果我只是将 UINavigationBar 子类化并告诉我的故事板中的 Navbar 属于此类,是否可以...不进行分类?我的方法有哪些优点/缺点?
  • 你也可以这样做,是的。
猜你喜欢
  • 2012-12-11
  • 1970-01-01
  • 2023-04-07
  • 1970-01-01
  • 1970-01-01
  • 2011-07-31
  • 2011-05-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多