Pri*_*ine 22 iphone uinavigationbar uinavigationcontroller ios
我一直在尝试更改我的应用程序的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
Run Code Online (Sandbox Code Playgroud)
但它没有用.我的下一个尝试是编写一个覆盖drawRect方法的CustomizedNavigationBar类.它看起来像那样:
CustomizedNavigationBar.h
#import <UIKit/UIKit.h>
@interface CustomizedNavigationBar : UINavigationBar
@end
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
在我定义NavigationBar的.xib文件中,我将类更改为新的CustomizedNavigationBar.但它仍然无法正常工作..
作为另一个测试,我下载了一个示例项目,其中应该更改背景图像.但即使使用该示例代码也无法正常工作.
我究竟做错了什么?我正在使用IOS 5.我可以定义背景图像的任何建议或其他方式吗?
谢谢你的回答!
Cra*_*raz 77
从iOS 5开始,您应该使用以下-setBackgroundImage:forBarMetrics:方法:
[myNavbar setBackgroundImage:[UIImage imageNamed: @"UINavigationBarBackground.png"]
forBarMetrics:UIBarMetricsDefault];
Run Code Online (Sandbox Code Playgroud)
在Swift 4中:
navigationBar.setBackgroundImage(UIImage(named: "UINavigationBarBackground.png"),
for: .default)
Run Code Online (Sandbox Code Playgroud)
考虑到所有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
Run Code Online (Sandbox Code Playgroud)
我在像Utilities文件这样的公共场所使用这些代码.
希望这可以帮助.
| 归档时间: |
|
| 查看次数: |
33490 次 |
| 最近记录: |