如何使工具栏背景透明?

时间:2013-04-26 18:40:24

标签: ios objective-c uiview uitoolbar

我想让我的UIToolBar具有透明背景(类似于iBooks),但我没有设置translucent属性。

这是我的代码:

UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    NSMutableArray *toolBarItems = [[NSMutableArray alloc] init];
    [toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:nil action:nil]];
    [toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:@"Source" style:UIBarButtonItemStyleBordered target:nil action:nil]];
    [toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:@"Aa" style:UIBarButtonItemStyleBordered target:nil action:nil]];
    [toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:@"Rabbit" style:UIBarButtonItemStyleBordered target:nil action:nil]];
    toolBar.items = toolBarItems;
    toolBar.translucent = YES;
    [self.view addSubview:toolBar];

它仍然是这样的:

enter image description here

2 个答案:

答案 0 :(得分:23)

如果您想要工具栏  透明:

[toolBar setBackgroundImage:[[UIImage alloc] init] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];

如果您希望工具栏为半透明

[toolBar setBarStyle:UIBarStyleBlack];
toolBar.translucent = YES;

希望它对你有所帮助。

答案 1 :(得分:2)

一个选项是子类UIToolbar并覆盖draw方法,按钮将继续正常绘制:

@interface TransparentToolbar : UIToolbar 
{
}

@implementation TransparentToolbar

// drawRect stub, toolbar items will still draw themselves
- (void)drawRect:(CGRect)rect
{
    return;
}

@end