How to subclass UINavigationBar for a UINavigationController programmatically?(如何以编程方式为 UINavigationController 子类化 UINavigationBar?)
问题描述
我在 iOS4 的应用程序中使用自定义 drawRect 函数在 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.
当 navigationBar 时,如何用我在 UINavigationController 中的子类替换 UINavigationBar (因此它将与 iOS4 和 iOS5 兼容)code> 属性是只读的?
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 开始,通过使用 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何以编程方式为 UINavigationController 子类化 UINavigationBar?
基础教程推荐
- AdMob 广告未在模拟器中显示 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
