简体   繁体   English

更改UINavigationBar背景图像

[英]Changing the UINavigationBar background image

I've been trying to change the background image of the UINavigationBar of my application. 我一直在尝试更改我的应用程序的UINavigationBar的背景图像。 I tried several ways. 我尝试了几种方法。 First I added to my AppDelegate class the following code: 首先,我在AppDelegate类中添加了以下代码:

@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

But it wasn't working. 但它没有用。 My next try was to write a CustomizedNavigationBar Class which is overriding the drawRect method. 我的下一个尝试是编写一个覆盖drawRect方法的CustomizedNavigationBar类。 It looked like that: 它看起来像那样:

CustomizedNavigationBar.h CustomizedNavigationBar.h

#import <UIKit/UIKit.h>

@interface CustomizedNavigationBar : UINavigationBar

@end

CustomizedNavigationBar.m CustomizedNavigationBar.m

#import "CustomizedNavigationBar.h"

@implementation CustomizedNavigationBar

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

    NSLog(@"I got called!!!!!");
}

@end

In my .xib file where the NavigationBar is defined I changed the class to the new CustomizedNavigationBar. 在我定义NavigationBar的.xib文件中,我将类更改为新的CustomizedNavigationBar。 But still it is not working.. 但它仍然无法正常工作..

As another test I downloaded an example project where the background image should be changed. 作为另一个测试,我下载了一个示例项目,其中应该更改背景图像。 But even with that sample code it was not working. 但即使使用该示例代码也无法正常工作。

What am I doing wrong? 我究竟做错了什么? I am using IOS 5. Any suggestions or other ways I could define a background image? 我正在使用IOS 5.我可以定义背景图像的任何建议或其他方式吗?

Thanks for your answers! 谢谢你的回答!

Starting in iOS 5 you should use the -setBackgroundImage:forBarMetrics: method: 从iOS 5开始,您应该使用-setBackgroundImage:forBarMetrics:方法:

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

And in Swift 4 : Swift 4中

navigationBar.setBackgroundImage(UIImage(named: "UINavigationBarBackground.png"),
               for: .default)

Considering all iOS versions, this seems to be accomplishing both Custom background image and Custom size of UINavigationBar: 考虑到所有iOS版本,这似乎是完成自定义背景图像和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. 我在像Utilities文件这样的公共场所使用这些代码。

Hope this helps. 希望这可以帮助。

Just try with this code.. In your implmentation (.m) file. 只需尝试使用此代码..在您的implmentation(.m)文件中。

#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 @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..... 以上代码仅适用于IOS 4.如果您使用IOS 5,则使用.....

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

对于iOS 4来说,有一个简单的解决方案,例如:

nvc.navigationBar.layer.contents = (id)img.CGImage;

You can also try this for Navigationbar Background. 您也可以尝试使用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. 谢谢。

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). 但是添加了一个以导航栏为中心的背景图像,这有时更有意义(我正在寻找)。

You can add UIImageview to navigation bar as follows UINavigationBar navBar = [[UINavigationBar alloc]init]; 您可以将UIImageview添加到导航栏,如下所示:UINavigationBar navBar = [[UINavigationBar alloc] init]; [navBar addSubview: imageview]; [navBar addSubview:imageview]; [navBar release]; [navBar发布];

You can check the post : iPhone - NavigationBar Custom Background 您可以查看帖子: iPhone - NavigationBar自定义背景

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM