Semitransparent background image (PNG24) for UINav

2019-09-08 21:38发布

问题:

I'm using a category to implement a custom background for UINavigationBar.

@implementation UINavigationBar (UINavigationBarCategory)

- (void)drawRect:(CGRect)rect 
{
    UIImage *img  = [UIImage imageNamed: @"TopNav-YellowRule.png"];
    [img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];

}
@end

The image that I am using is a semitransparent PNG24. When the app loads the background of the UINavigationBar shows up just fine but is not semitransparent - I cannot see the views that are below the UINavigationBar.

When I rotate the device, however, the image is suddenly semitransparent and works just fine. Any ideas what is happening on rotate that is allow the image to display as intended? Is there a way to fix it so that it displays properly on initial load, before a rotation?

回答1:

Try adding this after @end. See if it helps

navgationController.navigationBar.backgroundColor = [UIColor clearColor];