iphone - 更改 UINavigationBar 背景图片

标签 iphone ios uinavigationcontroller uinavigationbar

我一直在尝试更改我的应用程序的 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/

相关文章:

ios - 遍历 NSMutableArray 以在数组中添加对象

ios - UIPageViewController 内的 UINavigationController 垂直滚动,导航栏高度损坏

iphone - iphone 4.3导航栏问题

swift - 我如何使用 navigationController?.popViewController(动画 : true) inside of a closure with no warnings and errors?

iphone - 使用 ARC 的优点和缺点是什么?

iPhone IB导出

iOS 输入麦克风输出蓝牙设备

ios - 为什么 DetailViewController 会弹出到带有选项卡栏和导航 Controller 的第一个 DetailViewController?

php - xcode mysql php错误

iphone - 有什么方法可以消除加速度计数据中沿重力轴的小偏差