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

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

问题描述

为了自定义我的 UINavigationController navigationBar 的方面,我被告知要继承 UINavigationBar class。

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.

现在,我的问题是,如何分配到 UINavigationController ,我的 CustomNavigationBar 实例?我不能使用 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,但子类 UINavigationController 不会被Apple推荐,所以我有点困惑。

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

Subview UIView(CORRECTION:UINavigationController)带有一个笔尖,使用(例如,iPhone纵向)一个imageview作为背景(可以有一个渐变),然后左右两侧的自定义按钮,都连接到你的相关响应者功能。只要您将每个视图控制器等的导航栏视图添加为子视图,每次将新的导航栏视图推送到堆栈时,您都会覆盖Apple的标准导航栏。 (记得将他们设置为隐藏在app delegate中)

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)

编辑:

NavigationBar.h

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

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只传递了一些东西 - 一个枚举在我的app delegate中定义,其中包含所有可能的视图控制器的名称。我们在自定义导航栏类​​中解释此枚举,并在所有可能的控制器之间切换,然后设置其按钮和标题以及使用该信息触发的操作。屏幕宽度很重要 - 如果您在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天全站免登陆