Custom uinavigatonbar exception

旧街凉风 提交于 2020-01-04 06:20:25

问题


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

Now I'd like to add exception to show the normal uinavigation bar when i load an specific viewcontroller into de navigationcontroller.

How can I code that exception?


回答1:


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

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

#import <objc/runtime.h>

And in your view controller (in viewDidLoad for example), add

object_setClass(self.navigationController.navigationBar, [CustomNavigationBar class]);


来源:https://stackoverflow.com/questions/4593816/custom-uinavigatonbar-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!