ios 更改 UINavigationBar 背景图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7764309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:52:31  来源:igfitidea点击:

Changing the UINavigationBar background image

iphoneiosuinavigationcontrolleruinavigationbar

提问by Prine

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:

我一直在尝试更改应用程序 UINavigationBar 的背景图像。我尝试了几种方法。首先,我在 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 drawRectmethod. It looked like that:

但它不起作用。我的下一个尝试是编写一个自定义导航栏类,它覆盖了drawRect方法。它看起来像这样:

CustomizedNavigationBar.h

自定义导航栏.h

#import <UIKit/UIKit.h>

@interface CustomizedNavigationBar : UINavigationBar

@end

CustomizedNavigationBar.m

自定义导航栏.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. But still it is not working..

在定义 NavigationBar 的 .xib 文件中,我将类更改为新的 CustomizedNavigationBar。但它仍然不起作用..

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!

感谢您的回答!

回答by Craz

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)

回答by theunexpected1

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.

我在一个常见的地方使用这样的代码,比如一个实用程序文件。

Hope this helps.

希望这可以帮助。

回答by Ajay Sharma

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

只需尝试使用此代码.. 在您的实现 (.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 根视图控制器

- (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];

回答by Pavel Sich

Well for iOS 4 there is a simple solution, like:

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

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

回答by Minakshi

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

您可以将 UIImageview 添加到导航栏,如下所示 UINavigationBar navBar = [[UINavigationBar alloc]init]; [navBar addSubview: imageview]; [导航栏发布];

You can check the post : iPhone - NavigationBar Custom Background

您可以查看帖子: iPhone - NavigationBar 自定义背景

回答by Gajendra K Chauhan

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.

谢谢。

回答by karim

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).

这不会改变整个导航栏背景。但是添加了一个以导航栏为中心的背景图像,这有时更有意(我正在寻找)。