![](/img/trans.png)
[英]Add a background image to tables in a navigation view in a tabbed application
[英]Background image for navigation view
我在正确显示导航视图的背景图像时遇到问题。 这是图片:
这是代码:
- (id)initWithStyle:(UITableViewStyle)style {
if (self = [super initWithStyle:style]) {
UIImage *image = [UIImage imageNamed: @"bg_table_active.png"];
UIImageView *imageview = [[UIImageView alloc] initWithImage: image];
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]
initWithTitle:NSLocalizedString(@"Settings", @"")
style:UIBarButtonItemStyleDone
target:self
action:@selector(GoToSettings)];
self.navigationItem.titleView = imageview;
self.navigationItem.rightBarButtonItem = addButton;
self.navigationItem.hidesBackButton = TRUE;
}
return self;
}
如何将图片拉伸到整个导航视图?
我在我的应用程序中这样做。 在AppDelegate中我有这个代码:
@implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect
{
UIImage *image = [UIImage imageNamed: @"custom_nav_bar.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end
我修改了Mike Rundle的版本,以便在必要时设置自定义图像。 我还合并了40lb-suit-of-bees建议的改变。 初始化期间需要调用initImageDictionary:
//UINavigationBar+CustomImage.h
#import <Foundation/Foundation.h>
@interface UINavigationBar(CustomImage)
+ (void) initImageDictionary;
- (void) drawRect:(CGRect)rect;
- (void) setImage:(UIImage*)image;
@end
//UINavigationBar+CustomImage.m
#import "UINavigationBar+CustomImage.h"
//Global dictionary for recording background image
static NSMutableDictionary *navigationBarImages = NULL;
@implementation UINavigationBar(CustomImage)
//Overrider to draw a custom image
+ (void)initImageDictionary
{
if(navigationBarImages==NULL){
navigationBarImages=[[NSMutableDictionary alloc] init];
}
}
- (void)drawRect:(CGRect)rect
{
NSString *imageName=[navigationBarImages objectForKey:[NSValue valueWithNonretainedObject: self]];
if (imageName==nil) {
imageName=@"header_bg.png";
}
UIImage *image = [UIImage imageNamed: imageName];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
//Allow the setting of an image for the navigation bar
- (void)setImage:(UIImage*)image
{
[navigationBarImages setObject:image forKey:[NSValue valueWithNonretainedObject: self]];
}
@end
你也可以使用它
if([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {
//iOS 5 new UINavigationBar custom background
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbg_ForiPhone5_Imagename.png"] forBarMetrics: UIBarMetricsDefault];
} else {
[self.navigationController.navigationBar insertSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"navbg_ForOtherIphone_Imagename.png"]] atIndex:0];
}
`
Mike Rundle和Casebash的代码很棒。 我使用[NSValue valueWithNonretainedObject:self]来避免copyWithZone错误。 在NSValue对象中包装self允许将其复制到navigationBarImages字典中。
- (void)drawRect:(CGRect)rect
{
NSString *imageName=[navigationBarImages objectForKey:[NSValue valueWithNonretainedObject:self]];
...}
- (void)setImage:(NSString*)image
{
[navigationBarImages setObject:image forKey:[NSValue valueWithNonretainedObject:self]];
}
在该链接中查看图1 - 在导航栏上设置backgroundImage而不是导航项更好吗?
UIImage *image = [UIImage imageNamed: @"navigator.png"];
[_homeNavigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
遗憾的是,不支持在iPhone OS 3.0或任何先前版本的导航栏中使用自定义背景图像。 自定义外观的唯一方法是设置样式和色调颜色。 我知道,不完美。
在您的代码中,您试图将导航栏的标题视图拉伸到“右下角”。 但这是不可能的,因为导航栏的三个视图(后退按钮,标题和右按钮)应该在同一层中并且被调整为不重叠。 这是一个功能。
我知道有很多第三方应用程序可以更改背景图像,但它们“黑客”系统并使用不受支持的私有API:或导航栏内部数据结构的假设。 在未来版本的iPhone OS中,这些程序很可能会失败(崩溃或显示不正确)。
你很可能不想搞砸这个。 接受这样的事实:您还不能(还)在导航栏中拥有自定义背景图像。 我知道,这很疼。 但是,如果您破解系统并且您的应用程序在未来版本的操作系统中失败,Apple将从应用程序商店中取出应用程序,在您更改应用程序之前,您将失去所有收入。 这是你的电话......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.