如何将我的 CustomNavigationBar 分配给 UINavigationController?

发布于 2024-11-16 03:03:54 字数 428 浏览 3 评论 0原文

为了自定义 UINavigationControllernavigationBar 方面,我被告知要对 UINavigationBar 类进行子类化。

我需要更改栏的高度,放置图像而不是标题......等等。我不能仅使用 UINavigationBar 属性来做到这一点。

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

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

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

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.

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

偏爱你一生 2024-11-23 03:03:54

具体取决于您想要导航栏的自定义程度,以下内容可能会帮助您:

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

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

2)一个完全自定义的导航栏子

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


编辑:

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

请随时通知我:)

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

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) A totally custom nav bar

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)


Edit:

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;
}

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.

Keep me posted :)

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