如何将我的CustomNavigationBar分配给UINavigationController?

为了定制我的UINavigationControllernavigationBar UINavigationController ,我被告知UINavigationBar类的子类。

我需要改变酒吧​​的高度,放置一个图像,而不是标题..等我不能只使用UINavigationBar属性。

现在,我的问题是,如何分配给我的CustomNavigationBar实例的UINavigationController ? 我不能使用UINavigationController.navigationBar因为它是只读的。

唯一的办法似乎加载一个xib,但UINavigationController子类不是由苹果公司推荐的,所以我有点困惑。

完全取决于你想要的导航栏的自定义,以下可能会帮助你:

1)栏中的图像,replace标题文本。 (self是一个UIViewController)

 UIImageView *titleImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image.png"]]; self.navigationItem.titleView = titleImg; [titleImg release]; 

2)完全自定义的导航栏

子视图UIView(CORRECTION:UINavigationController)与一个笔尖,使用(例如,iPhone的肖像方向)一个ImageView作为背景(可以有一个渐变),然后在左侧和右侧的自定义button,都挂钩到相关响应function。 只要你把这个导航栏视图作为一个子视图添加到每个视图控制器等等,每当一个新的视图控制器被推入堆栈时,你就会覆盖苹果的标准导航栏。 (记得将它们设置为隐藏在应用程序委托中)


编辑:

NavigationBar.h

 @interface NavigationBar: UIViewController { IBOutlet UIImageView* navigationBarImgView; // remember to set these 4 IBOutlets as properties with (nonatomic, retain) and synthesize them. IBOutlet UILabel* controllerHeader; IBOutlet UIButton* rightButton; IBOutlet UIButton* leftButton; eController controller; int screenWidth; } 

NavigationBar.m

 -(id)initNavigationBar: (NSString*)name bundle:(NSBundle*)bundle: (eController)controllerName: (int)inScreenWidth { [super initWithNibName:name bundle:bundle]; controller = controllerName; screenWidth = isScreenWidth; return self; } 

你可以在这里看到我的init只传递了一些东西 – 在我的应用代理中定义的一个枚举,它包含所有可能的视图控制器的名字。 我们在我的自定义导航栏类​​中解释这个枚举,然后在所有可能的控制器之间切换,然后设置它们的button和标题以及使用这些信息触发的操作。 屏幕宽度很重要 – 尽pipe如果您在IB中创build条形图,则可能不需要此设置,但请设置它的大小以填充为两个方向分配的空间。

让我贴:)