UINavigationBar's drawRect is not called in iOS 5.0

后端 未结 7 1876
清歌不尽
清歌不尽 2020-11-28 07:59

I\'ve overrided(placed in category, or swizzled) UINavigationBar\'s drawRect to show custom background. In iOS 5 it\'s not working. What should I do?

7条回答
  •  借酒劲吻你
    2020-11-28 08:08

    Here's a less-ugly solution that works for both iOS4 and 5:

    @implementation UINavigationBar (CustomBackground)
    
    - (UIImage *)barBackground
    {
        return [UIImage imageNamed:@"top-navigation-bar.png"];
    }
    
    - (void)didMoveToSuperview
    {
        //iOS5 only
        if ([self respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)])
        {
            [self setBackgroundImage:[self barBackground] forBarMetrics:UIBarMetricsDefault];
        }
    }
    
    //this doesn't work on iOS5 but is needed for iOS4 and earlier
    - (void)drawRect:(CGRect)rect
    {
        //draw image
        [[self barBackground] drawInRect:rect];
    }
    
    @end
    

提交回复
热议问题