如何以UINavigationController为UINavigationControl编程子类化? [英] How to subclass UINavigationBar for a UINavigationController programatically?
问题描述
我使用自定义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
当 navigationBar
属性是只读的时,在 UINavigationController
(因此它将与iOS4和iOS5兼容) p>
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,所以添加 UINavigationBar
到NIB,并通过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开始,现在可以很容易地完成,不需要搅拌或混淆其他类, code> 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为UINavigationControl编程子类化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!