Change Navigation bar Background image on each navigation

后端 未结 6 1735
梦如初夏
梦如初夏 2020-12-09 13:38

I am working on iPhone app. I have added Navigation bar Background image

With interface: -

@interface UINavigationBar (backgroundImageWithTitle)

6条回答
  •  悲&欢浪女
    2020-12-09 14:04

    Using (SDK 3.2.5.) - In one of my Apps I needed navigationBar with custom image both in landscape and portrait.

    What I did was:

    @implementation UINavigationBar (BackgroundImage)
    
    - (void)drawRect:(CGRect)rect 
    {
        UIImage *image;
    
        if(self.frame.size.width == 320) //for iPhone - portrait will have 320 pix width.
        {
            image = [UIImage imageNamed: @"portraitNavigationBar.png"];
        }
        else
        {
            image = [UIImage imageNamed: @"landscapeNavigationBar.png"];
        }
    
        [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    }
    
    @end
    

    Luckily my app supports rotation - so, when rotating - navigation bar automatically redraws itself.

    But to manually redraw it - you can also call :

    [navigationController.navigationBar setNeedsDisplay];
    

提交回复
热议问题