我能在我的iPhone应用中为UIToolBar添加自定义背景吗?

25

是否可以为UIToolBar提供自定义背景图片而非通常的淡蓝色/黑色渐变色?

我已经尝试给视图设置背景并设置UIToolBar的不透明度,但这也会影响其上的任何UIBarButton的不透明度。

10个回答

40

在这里回答自己的问题!!! 覆盖drawRect函数并创建UIToolbar的实现即可解决问题 :)

    @implementation UIToolbar (CustomImage)
- (void)drawRect:(CGRect)rect {
    UIImage *image = [UIImage imageNamed: @"nm010400.png"];
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end

14
你正在为 UIToolbar 创建一个“类别”(category)。 - gerry3
9
谢谢,这个方法有效,但我认为最好是通过子类化UIToolbar来实现。 - HansPinckaers
1
我相信所有其他元素都是子视图,因此不需要在drawRect中绘制它们。 - atticus
6
直接使用drawInRect:self.bounds会怎样? - user142019
但是在4.3版本中无法正常工作 :(. 它在之前的iOS 4.2中运行良好,但在4.3中不行。 - Ajay Sharma
显示剩余2条评论

16

UIToolbar继承自UIView。这个对我来说很有效:

[topBar insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:BAR_BKG_IMG]] autorelease] atIndex:0];

10

这是对loerto答案的稍作修改,在iOS 4和5上对我有效:

// Set the background of a toolbar
+(void)setToolbarBack:(NSString*)bgFilename toolbar:(UIToolbar*)toolbar {   
    // Add Custom Toolbar
    UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:bgFilename]];
    iv.frame = CGRectMake(0, 0, toolbar.frame.size.width, toolbar.frame.size.height);
    iv.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    // Add the tab bar controller's view to the window and display.
    if([[[UIDevice currentDevice] systemVersion] intValue] >= 5)
        [toolbar insertSubview:iv atIndex:1]; // iOS5 atIndex:1
    else
        [toolbar insertSubview:iv atIndex:0]; // iOS4 atIndex:0
    toolbar.backgroundColor = [UIColor clearColor];
}

2
在iOS 5中,有新的API允许自定义控件。不需要插入子视图。此外,检查系统版本不是推荐的做法,respondsToSelector是一个更强大的替代方案。 - Fabio
我认为检查系统版本没有任何问题。那么,如何理解 respondsToSelector 更加健壮呢? - Arie Litovsky
@diablosnuevos - 如果你现在检查系统版本,每次新版本发布时都必须不断修改代码。respondsToSelector与系统版本无关。也就是说,这种方法将响应insertSubview而不考虑版本,因此在这里不适用respondsToSelector。 - Jon

9

这是我用于iOS 4和5兼容性的方法:

if ([toolbar respondsToSelector:@selector(setBackgroundImage:forToolbarPosition:barMetrics:)]) {
    [toolbar setBackgroundImage:[UIImage imageNamed:@"toolbar-background"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
} else {
    [toolbar insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"toolbar-background"]] autorelease] atIndex:0];
}

嗨,Christopher,我正在实现这个解决方案,但我仍然可以看到工具栏上的高亮(覆盖工具栏顶部一半的苹果标准高亮)。您知道是否有一种方法可以关闭该突出显示吗? - Bocaxica
@MadOxyn 奇怪,我没有看到那个亮点。你在哪个iOS版本上看到的? - Christopher Pickslay
我在iOS4和5上都看到了这个问题,但现在我遇到了它。我很愚蠢,有一个分页控件覆盖在它上面,带有半透明的白色背景,这导致了那个高亮。我没有意识到这一点。你的回复让我重新思考;)谢谢。 - Bocaxica

7
只需将此代码添加到您的-(void)viewDidLoad{}中即可。
[toolBarName setBackgroundImage:[UIImage imageNamed:@"imageName.png"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];

2

如果您使用idimmu的答案,并希望您的barbuttonitems具有颜色而不是默认值,您还可以将以下几行代码添加到您的类别中:

UIColor *color = [UIColor redColor];
self.tintColor = color;

2

自iOS5起,您可以使用外观API:

[[UIToolbar appearance] setBackgroundImage:[UIImage imageNamed:@"navbar_bg"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];

1
为了符合iOS 5的要求,您可以这样做:
-(void) addCustomToolbar {

    // Add Custom Toolbar
    UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"customToolbar.png"]];
    img.frame = CGRectMake(-2, -20, img.frame.size.width+4, img.frame.size.height);

    // Add the tab bar controller's view to the window and display.

    if( SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO( @"5.0" ) )
       [self.tabBarController.tabBar insertSubview:img atIndex:1]; // iOS5 atIndex:1
    else
      [self.tabBarController.tabBar insertSubview:img atIndex:0]; // iOS4 atIndex:0

    self.tabBarController.tabBar.backgroundColor = [UIColor clearColor];

    // Override point for customization after application launch.
    [self.window addSubview:tabBarController.view];

}

非常好!由于某些原因,类别方法对我不起作用。 - Chris
刚刚不得不更改 if 语句为:if([[[UIDevice currentDevice] systemVersion] intValue] >= 5)。 - Chris

0

这一个对我来说很好用:

ToolbarOptions *tbar = [[ToolbarOptions alloc] init];
[tbar setToolbarBack:@"footer_bg.png" toolbar:self.toolbarForPicker];
[tbar release];

#import <Foundation/Foundation.h>
@interface ToolbarOptions : NSObject {

}
-(void)setToolbarBack:(NSString*)bgFilename toolbar:(UIToolbar*)toolbar;
@end

#import "ToolbarOptions.h"


@implementation ToolbarOptions

-(void)setToolbarBack:(NSString*)bgFilename toolbar:(UIToolbar*)bottombar {   
// Add Custom Toolbar
UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:bgFilename]];
iv.frame = CGRectMake(0, 0, bottombar.frame.size.width, bottombar.frame.size.height);
iv.autoresizingMask = UIViewAutoresizingFlexibleWidth;
// Add the tab bar controller's view to the window and display.
if([[[UIDevice currentDevice] systemVersion] intValue] >= 5)
    [bottombar insertSubview:iv atIndex:1]; // iOS5 atIndex:1
else
    [bottombar insertSubview:iv atIndex:0]; // iOS4 atIndex:0
bottombar.backgroundColor = [UIColor clearColor];
}

@end

0

您可以使用一种类别来实现这个功能,它基本上为UIToolBar添加了一个新属性。覆盖drawRect可能有效,但不一定是未来的解决方案。同样的策略用于自定义UINavigationBar在iOS 6中停止工作。

以下是我的做法。

.h文件

@interface UIToolbar (CustomToolbar)
@property (nonatomic, strong) UIView *customBackgroundView;
@end

.m文件

#import "CustomToolbar.h"
#import
static char TIToolbarCustomBackgroundImage;
@implementation UIToolbar (CustomToolbar)
- (void)setCustomBackgroundView:(UIView *)newView { UIView *oldBackgroundView = [self customBackgroundView]; [oldBackgroundView removeFromSuperview];
[self willChangeValueForKey:@"tfCustomBackgroundView"]; objc_setAssociatedObject(self, &TIToolbarCustomBackgroundImage, newView, OBJC_ASSOCIATION_RETAIN); [self didChangeValueForKey:@"tfCustomBackgroundView"];
if (newView != nil) { [self addSubview:newView]; } }
- (UIView *)customBackgroundView { UIView *customBackgroundView = objc_getAssociatedObject(self, &TIToolbarCustomBackgroundImage);
return customBackgroundView; }
@end

在您的视图控制器代码中,例如viewDidLoad

    if (self.navigationController.toolbar.customBackgroundView == nil) {
        self.navigationController.toolbar.customBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"navigation_bar_background.png"]];
        self.navigationController.toolbar.customBackgroundView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接