Optional setting background image of navigation bar

I need to draw a background image or set the hue color in the navigation bar, but I also need the option to display the navigation bar as usual. I am currently using a category for support. If my application does not specify a background image, what can I do instead to make the drawRect method normally execute?

those. -

@implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
    if(hasImage){
        UIImage *img = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://myimageurl.com/img.jpg"]]];
        [img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    }else{
        ??????
    }
}
@end
+3
source share
2 answers

- , , . , Googling , UINavigationBar. , . .

myUIImageView.userInteractionEnabled = ; [myNavController.navigationBar addSubview: myUIImageView]; [myNavController.navigationBar sendSubviewToBack: myUIImageView];

/ , drawRect , swizzle . .

+1

UINavigationBar, drawRect:, [super drawRect:rect], .

, , UINavigationBar .

, , "" , . , , super, .

"" : http://cocoawithlove.com/2008/03/supersequent-implementation.html

( : C?)

0

Source: https://habr.com/ru/post/1746437/


All Articles