开发者

overriding UINavigationBar drawRect function doesnt work

开发者 https://www.devze.com 2023-03-22 18:41 出处:网络
I have a simple app with a nav bar controller and I would like to change its navigation bar while overriding the drawRect function. I read in many places here that all I need to do is just to paste th

I have a simple app with a nav bar controller and I would like to change its navigation bar while overriding the drawRect function. I read in many places here that all I need to do is just to paste the code below above my appDelegate. Sadly, it doesnt seem to do anything for me. I tried as a start to just have the color of the nav bar changed, before i try to add an image to it, but again, nothing happens. What am i missing here

I tired to insert this code above the AppDelegate:

@implementation UINavigationBar (UINavigationBarCategory)

- (void)drawRect:(CGRect)rect {
 UIColor *color = [UIColor redColor];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColor(context, CGColorGetComponents( [color CGColor]));
CGContextFillRect(context, rect);
  }
 @end

And with the b开发者_如何学Pythonackground image:

@implementation UINavigationBar (UINavigationBarCategory)

- (void)drawRect:(CGRect)rect {
// Drawing code 
UIImage *img = [UIImage imageNamed: @"13.png"];
[img drawInRect:CGRectMake(0, 0, 320, self.frame.size.height)];    
}
@end

Thanks for the help!


This code declares a category on UINavigationBar and redefines the behaviour of its drawRect: method. Try to put the code snippet (second one) at the end of the .m file of the app delegate.

Although the first should work, another more clean option is to write the category in a separate file you will include in the app delegate.

Also make sure the picture you are loading exists in the app bundle.


Ok - solution found! This doesnt seem to work on the iPhone 5.0 simulator!

0

精彩评论

暂无评论...
验证码 换一张
取 消