自定义uinavigatonbar异常 [英] Custom uinavigatonbar exception
问题描述
我通过这种方式制作了一个自定义的uinavigation栏:
I've made a custom uinavigation bar this way:
@implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
UIImage *img = [UIImage imageNamed: @"navigation_background.png"];
[img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end
现在,当我将特定的viewcontroller加载到de navigationcontroller中时,我想添加例外以显示正常的uinavigation栏.
Now I'd like to add exception to show the normal uinavigation bar when i load an specific viewcontroller into de navigationcontroller.
如何编码该异常?
推荐答案
我认为对于 UINavigationBar
上的类别,这是不可能的.我会建议一个子类,例如 CustomNavigationBar
I don't think that's possible with a category on UINavigationBar
.
I would suggest a subclass, say CustomNavigationBar
.h
#import <UIKit/UIKit.h>
@interface CustomNavigationBar : UINavigationBar {
}
@end
.m
#import "CustomNavigationBar.h"
@implementation CustomNavigationBar
- (void)drawRect:(CGRect)rect {
UIImage *img = [UIImage imageNamed: @"navigation_background.png"];
[img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end
棘手的事情是,您永远不会自己创建 UINavigationBar
,这全部由UIKit处理.要仅更改某些栏的外观,您必须在运行时手动更改它们的类.
The tricky thing is that you never create a UINavigationBar
yourself, it's all handled by UIKit. To change the appearance of some bars only, you'll have to manually change their class at runtime.
导入
#import <objc/runtime.h>
然后在您的视图控制器中(例如在viewDidLoad中)添加
And in your view controller (in viewDidLoad for example), add
object_setClass(self.navigationController.navigationBar, [CustomNavigationBar class]);
这篇关于自定义uinavigatonbar异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!