我用一个TabBarController创建了一个应用程序,4个TabBarItems,每个TabBarItem都有自己的NavigationController文件(所有在IB中创建的文件)。
我正在使用drawRect函数来设计我的navigationBar:
@implementation UINavigationBar (customImage)
-(void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed:@"MyImage.png"];
[image drawInRect:CGRectMake(0,0, self.frame.size.width,self.frame.size.height)];
}
@end问题是,我的应用程序中的每个ViewController的NavigationBar都会改变,我想要的是只为一个TabBarItem绘制NavigationBar,而为其他控制器使用默认的navigationBar。
如何做到这一点?
谢谢,
发布于 2010-04-01 18:01:04
好的,
他花了我很长时间,但我找到了解决办法。通过使用swizzling方法,我可以做任何我想做的事情。有关如何实现它的更多信息,请查看此link。
发布于 2010-04-01 11:52:14
在这种情况下,使用类别可能不是最好的想法(如果使用类别覆盖drawRect:,所有的UINavigationController都会受到影响)。最好是将UINavigationController子类化,并且只在您想要的选项卡中使用自定义子类。
编辑:子类化UINavigationController相当简单:
(在.h文件中)
@interface MyCustomNavigationController : UINavigationController {
}
@end(在.m文件中)
@implementation MyCustomNavigationController
-(void)drawRect:(CGRect)rect {
//custom drawing here
}
@end然后,在适当的代码中使用MyCustomNavigationController而不是UINavigationController。类别(在示例中使用)对于向现有类添加方便的方法很有用,但在替换现有方法时可能会很危险(因为您的自定义方法将替换所有用途的原始方法)。
https://stackoverflow.com/questions/2558012
复制相似问题