Changing the UINavigationBar background image

后端 未结 7 440
时光取名叫无心
时光取名叫无心 2020-12-01 08:29

I\'ve been trying to change the background image of the UINavigationBar of my application. I tried several ways. First I added to my AppDelegate class the following code:

相关标签:
7条回答
  • 2020-12-01 08:34

    Just try with this code.. In your implmentation (.m) file.

    #import "RootViewController.h"
    
    @implementation UINavigationBar (CustomImage)
    - (void)drawRect:(CGRect)rect {
        UIImage *image = [UIImage imageNamed: @"navheader.png"];
        [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    }
    @end
    

    @implementation RootViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
        self.title=@"You are Done.";
    }
    

    This has worked in excellent way for me.

    Above Code Worked for only IOS 4. if you use the IOS 5 then use.....

     [myNavbar setBackgroundImage:[UIImage imageNamed:
     @"UINavigationBarBackground.png"] 
                        forBarMetrics:UIBarMetricsDefault];
    
    0 讨论(0)
  • 2020-12-01 08:34

    You can also try this for Navigationbar Background.

    UINavigationBar *navigationBar = self.navigationController.navigationBar;
    UIImage *image = [UIImage imageNamed: @"NavBar-Wood.png"];
    [navigationBar setBackgroundImage:image forBarMetrics: UIBarMetricsDefault];
    self.navigationController.navigationBar.tintColor = [UIColor brownColor];
    

    Thanks.

    0 讨论(0)
  • 2020-12-01 08:37

    Starting in iOS 5 you should use the -setBackgroundImage:forBarMetrics: method:

    [myNavbar setBackgroundImage:[UIImage imageNamed: @"UINavigationBarBackground.png"] 
                   forBarMetrics:UIBarMetricsDefault];
    

    And in Swift 4:

    navigationBar.setBackgroundImage(UIImage(named: "UINavigationBarBackground.png"),
                   for: .default)
    
    0 讨论(0)
  • 2020-12-01 08:41

    Considering all iOS versions, this seems to be accomplishing both Custom background image and Custom size of UINavigationBar:

    @interface CustomNavigationBar : UINavigationBar
    @end
    
    @implementation CustomNavigationBar
    -(void) drawRect:(CGRect)rect 
    {
        UIImage *image = [UIImage imageNamed: @"navigationBar"];
        [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    
        //for iOS5
        [self setBackgroundImage:[UIImage imageNamed: @"navigationBar"] forBarMetrics:UIBarMetricsDefault];
    }
    
    //for custom size of the UINavigationBar
    - (CGSize)sizeThatFits:(CGSize)size {
        CGSize newSize = CGSizeMake(320,31);
        return newSize;
    }
    
    @end
    

    I use such codes in a common place like a Utilities file.

    Hope this helps.

    0 讨论(0)
  • 2020-12-01 08:46

    You can add UIImageview to navigation bar as follows UINavigationBar navBar = [[UINavigationBar alloc]init]; [navBar addSubview: imageview]; [navBar release];

    You can check the post : iPhone - NavigationBar Custom Background

    0 讨论(0)
  • 2020-12-01 08:47

    Another way to set an image in navigation bar is,

    UIImage* logo = [ UIImage imageNamed:@"Logo.png" ];
    UIImageView* logoView = [[[UIImageView alloc] initWithImage:logo] autorelease];
    self.navigationItem.titleView = logoView;
    

    This will not change whole navigation bar background. But adds a background image centered on the navigation bar, which is sometime more intended (what I was looking for).

    0 讨论(0)
提交回复
热议问题