我可以在我的iPhone应用程序中为UIToolBar提供自定义背景吗?

时间:2009-12-21 16:28:48

标签: iphone uitoolbar

是否可以从图像中为UIToolBar提供自定义背景,而不是通常的有色蓝/黑淡出?

我尝试为视图提供背景并设置UIToolBar的不透明度,但这也会影响其上任何UIBarButtons的不透明度。

10 个答案:

答案 0 :(得分: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

答案 1 :(得分:16)

UIToolbar继承自UIView。这对我有用:

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

答案 2 :(得分:10)

loreto答案的略微修改版本,适用于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];
}

答案 3 :(得分: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];
}

答案 4 :(得分:7)

只需将此内容添加到-(void)viewDidLoad{}

即可
[toolBarName setBackgroundImage:[UIImage imageNamed:@"imageName.png"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];

答案 5 :(得分:2)

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

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

答案 6 :(得分:2)

您可以使用iOS5以来的Appearance API:

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

答案 7 :(得分: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];

}

答案 8 :(得分: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

答案 9 :(得分: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;
    }