如何将我的 CustomNavigationBar 分配给 UINavigationController? [英] How to assign my CustomNavigationBar to a UINavigationController?

查看:13
本文介绍了如何将我的 CustomNavigationBar 分配给 UINavigationController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了自定义 UINavigationControllernavigationBar 方面,我被告知要继承 UINavigationBar 类.

In order to customize the aspect of the navigationBar of my UINavigationController, I've been told to subclass the UINavigationBar class.

我需要更改栏的高度,放置图像而不是标题.. 等等.我不能只使用 UINavigationBar 属性.

I need to change the height of the bar, placing an image instead of the title.. etc. I can't do it just using UINavigationBar properties.

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

Now, my issue is, how to assign to the UINavigationController, my CustomNavigationBar instance ? I can't use UINavigationController.navigationBar because it is read-only.

唯一的方法似乎是加载一个xib,但是Apple不推荐子类化UINavigationController,所以我有点困惑.

The only way seems to load a xib, but subclassing UINavigationController is not reccomended by Apple, so I'm a bit confused.

推荐答案

具体取决于您想要导航栏的自定义方式,以下内容可能会对您有所帮助:

Depending exactly on how custom you want the nav bar, the following may help you:

1) 栏中的图像,替换标题文本.(self 是一个 UIViewController)

1) An image in the bar, replacing the title text. (self is a UIViewController)

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

2) 完全自定义的导航栏

2) A totally custom nav bar

带有笔尖的子视图 UIView(更正:UINavigationController),使用(例如,iPhone 纵向)图像视图作为背景(可能有渐变),然后在左侧和右侧自定义按钮,全部连接到您的相关响应者功能.只要您将此导航栏视图作为子视图添加到每个视图控制器等,每次将新视图推送到堆栈时,您就会覆盖 Apple 的标准导航栏.(记得在应用委托中将它们设置为隐藏)

Subview UIView (CORRECTION: UINavigationController) with a nib, using (for example, of iPhone portrait orientation) a imageview as the background (could have a gradient) and then custom buttons on the left and right side, all hooked up to your relevant responder functions. As long as you add this nav bar view as a subview to each view controller etc each time a new one is pushed to the stack, you would have overriden Apple's standard nav bar. (remember to set theirs to hidden in the app delegate)

导航栏.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;
}

导航栏.m

-(id)initNavigationBar: (NSString*)name bundle:(NSBundle*)bundle: (eController)controllerName: (int)inScreenWidth
{
    [super initWithNibName:name bundle:bundle];

    controller = controllerName;
    screenWidth = isScreenWidth;

    return self;
}

您可以在此处看到我的 init 只传递了一些东西 - 在我的应用程序委托中定义的枚举,其中包含所有可能的视图控制器的名称.我们在我的自定义导航栏类​​中解释这个枚举并在所有可能的控制器之间切换,然后设置它们的按钮和标题以及使用该信息触发的操作.屏幕宽度很重要 - 虽然如果您在 IB 中创建栏可能不需要它,但设置它的大小以填充两个方向的分配空间.

You can see my init here only passes in a few things - an enum which was defined in my app delegate which contains the names of all possible view controllers. We interpret this enum in my custom Navigation Bar class and switch between all the possible controllers, and then setup their buttons and titles and the actions that get fired off using that information. The screen width is important - although you may not need this if you create the bar in IB, set the size of it to fill up the allocated space for both orientations.

让我知道 :)

这篇关于如何将我的 CustomNavigationBar 分配给 UINavigationController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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