10

我在正确显示导航视图的背景图像时遇到问题。这是图片:

替代文字

这是代码:

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

如何使图片拉伸到整个导航视图?

4

8 回答 8

24

我在我的应用程序中正是这样做的。在 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
于 2009-08-11T22:01:17.457 回答
9

我修改了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
于 2010-01-12T02:51:53.047 回答
1

Mike Rundle 和 Casebash 的代码很棒。我使用 [NSValue valueWithNonretainedObject:self] 来避免 copyWithZone 错误。将 self 包装在 NSValue 对象中可以将其复制到 navigationBarImages 字典中。


- (void)drawRect:(CGRect)rect
{
    NSString *imageName=[navigationBarImages objectForKey:[NSValue valueWithNonretainedObject:self]];
...}


- (void)setImage:(NSString*)image
{
    [navigationBarImages setObject:image forKey:[NSValue valueWithNonretainedObject:self]];
}

于 2010-02-26T23:05:15.820 回答
1

http://foobarpig.com/iphone/uinavigationbar-with-solid-color-or-image-background.html

希望能帮助到你..

于 2010-04-22T06:55:25.750 回答
1

你也可以用这个

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

`

于 2012-12-12T05:59:42.527 回答
0

http://developer.apple.com/iphone/library/featuredarticles/ViewControllerPGforiPhoneOS/UsingNavigationControllers/UsingNavigationControllers.html#//apple_ref/doc/uid/TP40007457-CH7

查看该链接中的图 1 - 在导航栏而不是导航项上设置 backgroundImage 会更好吗?

于 2009-07-01T16:46:07.437 回答
0
 UIImage *image = [UIImage imageNamed: @"navigator.png"];

[_homeNavigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];

于 2013-06-04T08:16:30.780 回答
-2

不幸的是,不支持在 iPhone OS 3.0 或任何先前版本的导航栏中使用自定义背景图像。自定义外观的唯一方法是设置样式和色调颜色。不完美,我知道。

在您的代码中,您试图将导航栏的标题视图拉伸到“进入”右侧按钮。但这是不可能的,因为导航栏的三个视图(后退按钮、标题和右键)应该在同一层,并且被调整为不重叠。这是一个特点。

我知道有许多第三方应用程序会更改背景图像,但它们正在“入侵”系统并使用不受支持的私有 API:s 或导航栏内部数据结构的假设。这些程序在未来版本的 iPhone OS 中很可能会失败(崩溃或显示不正确)。

你很可能不想惹这个麻烦。接受这样一个事实,即您(还)不能在导航栏中拥有自定义背景图像。很痛,我知道。但是,如果您破解系统并且您的应用程序在未来版本的操作系统中失败,Apple 将从应用程序商店下架该应用程序,您将失去所有收入,直到您更改应用程序。是你的电话...

于 2009-07-03T12:47:25.460 回答