Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make UIToolbar's background transparent? [duplicate]

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

like image 603
Doug Smith Avatar asked Apr 26 '13 18:04

Doug Smith


2 Answers

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.

like image 153
Nishant Tyagi Avatar answered Oct 06 '22 09:10

Nishant Tyagi


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
like image 44
CSmith Avatar answered Oct 06 '22 08:10

CSmith