如何以编程方式为UINavigationController子类化UINavigationBar?

编程入门 行业动态 更新时间:2024-10-27 20:26:47
本文介绍了如何以编程方式为UINavigationController子类化UINavigationBar?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用自定义drawRect函数在iOS4中的应用程序上绘制 UINavigationBar ,它不使用图像,只使用CoreGraphics。

I'm using a custom drawRect function to draw on UINavigationBar across my application in iOS4, it doesn't use images, only CoreGraphics.

由于您无法在iOS5中的 UINavigationBar 类别中实现drawRect,Apple建议继承 UINavigationBar 。

Since you can't implement drawRect in UINavigationBar category in iOS5, Apple is suggesting to subclass UINavigationBar.

如何用我的子类替换 UINavigationBar 在 UINavigationController (当它只读取 navigationBar 属性时,它将与iOS4和iOS5兼容)

How is it possible to replace the UINavigationBar with my subclass in UINavigationController (so it'll be compatible with iOS4 and iOS5) when the navigationBar property is read only?

@property(nonatomic, readonly) UINavigationBar *navigationBar

我根本不在我的应用程序中使用XIB,因此向NIB添加 UINavigationBar 并通过InterfaceBuilder更改类是不是一个选项。

I'm not using XIBs in my application at all, so adding a UINavigationBar to a NIB and changing the class via InterfaceBuilder is not an option.

推荐答案

从iOS6开始,现在很容易实现,而不会通过使用 UINavigationControllers 方法 initWithNavigationBarClass:toolbarClass:

As of iOS6, this is now quite simple to accomplish without swizzling or messing with other classes by using UINavigationControllers method initWithNavigationBarClass:toolbarClass:

- (id)initWithNavigationBarClass:(Class)navigationBarClass toolbarClass:(Class)toolbarClass;

来自文档:

初始化并返回一个新创建的导航控制器,使用您的自定义栏子类。

Initializes and returns a newly created navigation controller that uses your custom bar subclasses.

为iOS6更新了答案。

更多推荐

如何以编程方式为UINavigationController子类化UINavigationBar?

本文发布于:2023-07-27 05:22:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1220583.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:子类   方式   UINavigationController   UINavigationBar

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!