How to add background image on iphone Navigation b

2019-01-05 10:34发布

I want to add an image background to my navigation bar.

Is it right?

//set custom background image
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"NavigationBackground.png"]];
[self.navigationBar insertSubview:backgroundView atIndex:0];
[backgroundView release];

12条回答
叼着烟拽天下
2楼-- · 2019-01-05 10:55

Your code alone won't do it, you'll have to write a category in order for it to work. There are two approaches regarding the way you should do it: the first one involves making the image a subview of your UINavigationBar and re-bringing it to front in each of your UIViewController's viewDidAppear method. This however has been reported having some issues with covering the right UIBarButtonItem. The other method involves overriding

- (void)drawRect:(CGRect)rect

and drawing the image there. Both of these are extensively covered in this blog discussion..

查看更多
迷人小祖宗
3楼-- · 2019-01-05 10:59

I would do this in the appdelegate something like

+ (void)Generalstyle {
    //navigationbar
    UINavigationBar *navigationBar = [UINavigationBar appearance];
    [navigationBar setBackgroundImage:[UIImage imageNamed:@"navigation.png"] forBarMetrics:UIBarMetricsDefault];

}

And in

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    [[self class] Generalstyle];

}

.h file:

+ (void) Generalstyle;
查看更多
神经病院院长
4楼-- · 2019-01-05 11:00

what i did was just created an UIImage with the image i wanted and added to the navigation bar like this.

UIImage *image = [UIImage imageNamed:@"yourImage.png"];

[[UINavigationBar appearance] setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
查看更多
成全新的幸福
5楼-- · 2019-01-05 11:02

While adding Navigation Bar background image, you should be very careful about its dimensions. Please make sure you have following dimensions for different screen sizes.

1) background.png => 320x44
2) background@2x.png => 640x88 // used for iPhone5 and for retina devices
3) background@3x.png => 1334x183 // used for iPhone6

Use following code to add background image and to avoid any tiling in Navigation Bar's background image.

[self.navigationController.navigationBar setBackgroundImage:[[UIImage imageNamed:@"background"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0) resizingMode:UIImageResizingModeStretch] forBarMetrics:UIBarMetricsDefault];
查看更多
甜甜的少女心
6楼-- · 2019-01-05 11:08

Here's the code from the link @luvieere mentioned. Paste this code into to the rootview controller just above @implementation rootviewController

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

As of iOS 5, there is an official way to do this. (see iOS Developer Library)

// someplace where you create the UINavigationController
if ([navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {
    UIImage *image = [UIImage imageNamed:@"NavigationBar.png"];
    [navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}

But still, retain the old code for backward compatibility unless you really want to ditch iOS 4 and below.

查看更多
来,给爷笑一个
7楼-- · 2019-01-05 11:09

Swift version :

let navBar = UINavigationBar.appearance();
navBar.setBackgroundImage(UIImage(named: "yourImageName"), forBarMetrics: .Default)

Updated Swift 3.2 :

let navBar = UINavigationBar.appearance();
        navBar.setBackgroundImage(UIImage(named: "yourImageName"), for: .default)
查看更多
登录 后发表回答