我一直在尝试更改我的应用程序的 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
但它没有用。我的下一个尝试是编写一个覆盖 drawRect 方法的 CustomizedNavigationBar 类。看起来像这样:
CustomizedNavigationBar.h
#import <UIKit/UIKit.h>
@interface CustomizedNavigationBar : UINavigationBar
@end
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
在定义 NavigationBar 的 .xib 文件中,我将类更改为新的 CustomizedNavigationBar。但是还是不行..
作为另一个测试,我下载了一个应该更改背景图像的示例项目。但即使使用该示例代码,它也无法正常工作。
我做错了什么?我正在使用 IOS 5。有什么建议或其他方法可以定义背景图像吗?
感谢您的回答!
最佳答案
从 iOS 5 开始,您应该使用 -setBackgroundImage:forBarMetrics:
方法:
[myNavbar setBackgroundImage:[UIImage imageNamed: @"UINavigationBarBackground.png"]
forBarMetrics:UIBarMetricsDefault];
在 Swift 4 中:
navigationBar.setBackgroundImage(UIImage(named: "UINavigationBarBackground.png"),
for: .default)
关于iphone - 更改 UINavigationBar 背景图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7764309/