5

I want to make my UIToolBar have a transparent background (similar to iBooks) but I'm having no luck with setting the translucent property.

Here's my code:

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

It still comes out like this:

enter image description here

Kal
  • 24,724
  • 7
  • 65
  • 65
Doug Smith
  • 29,668
  • 57
  • 204
  • 388
  • 1
    I believe you will need to set a transparent background image in order to do this. Just create a 1x1 pixel transparent png. – MTurner Apr 26 '13 at 18:48
  • @doug Smithh as uitoolbar is a subclass of uiview you can use it's layer property to make it transparent by changing the value of alpha – vignesh kumar Apr 26 '13 at 19:12
  • Have a look at: http://stackoverflow.com/questions/2468831/couldnt-uitoolbar-be-transparent#answer-3253738 – Shad Apr 26 '13 at 19:30

2 Answers2

23

If you want toolbar as Transparent :

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

and if you want toolbar as Translucent :

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

Hope it helps you.

Nishant Tyagi
  • 9,893
  • 3
  • 40
  • 61
2

One option is to subclass UIToolbar and override the draw method, the buttons will continue to draw themselves as normal:

@interface TransparentToolbar : UIToolbar 
{
}

@implementation TransparentToolbar

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

@end
CSmith
  • 13,318
  • 3
  • 39
  • 42