9

I wrote the following code to make my toolbar transparent.

[mtoolbar setBackgroundColor:[UIColor clearColor]];

How do I make UIToolbar transparent?

Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
er.mobileapp
  • 1,287
  • 4
  • 15
  • 21

7 Answers7

12

You can set the property translucent to YES and see if this helps.

Jonathan Sterling
  • 18,320
  • 12
  • 67
  • 79
Luke
  • 11,426
  • 43
  • 60
  • 69
  • I think we need to set the property yes – er.mobileapp Jun 06 '11 at 12:15
  • 1
    Edited :) Was just a quick guess, I've not worked directly with the toolbar before. – Luke Jun 06 '11 at 13:03
  • Its not working for me. The UIToolbar's background color is in black transparent now. But I need it in clear color. – Bharathi Feb 22 '12 at 05:54
  • See the comment that matches David H's answer. – Luke Feb 22 '12 at 07:30
  • Note: if a toolbar is added to the scene, it is *not* referenced by self.navigationController.toolbar - this is the default one owned by the navigationController (see http://stackoverflow.com/a/8753847/481207). – Matt Dec 09 '14 at 19:45
11
[self.toolbar setBackgroundImage:[UIImage new]
              forToolbarPosition:UIToolbarPositionAny
                      barMetrics:UIBarMetricsDefault];

[self.toolbar setBackgroundColor:[UIColor clearColor]];
Ievgen
  • 1,596
  • 16
  • 13
5

Setting the property translucent to YES will not work in iOS 5 and below. Here's how it can be done without subclassing toolbar:

const float colorMask[6] = {222, 255, 222, 255, 222, 255};
UIImage *img = [[UIImage alloc] init];
UIImage *maskedImage = [UIImage imageWithCGImage: CGImageCreateWithMaskingColors(img.CGImage, colorMask)];

[self.toolbar setBackgroundImage:maskedImage forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
jd.
  • 4,057
  • 7
  • 37
  • 45
4

Check the below code

[myToolbar setBarStyle:UIBarStyleBlack];
[myToolbar setTranslucent:YES];

Taken from

@Brandon Bodnár has answered in the below SO post.

Couldn't UIToolBar be transparent?

you could also use the different approach

Transparent UIToolBar

Community
  • 1
  • 1
Jhaliya - Praveen Sharma
  • 31,697
  • 9
  • 72
  • 76
2
for (UIView * sv in [toolBar subviews])
{
     [sv removeFromSuperview];
}

;) any iOs

Kappe
  • 9,217
  • 2
  • 29
  • 41
1

The following works in iOS 5 (and iOS 6 beta 4, although a slight top shadow is still visible there).

Please note: Making a UIToolbar or UINavigationBar transparent is rarely a good idea, and modifying Apple's UIKit elements in such a way is bound to break sooner or later.

TransparentToolbar.h

#import <UIKit/UIKit.h>

@interface TransparentToolbar : UIToolbar

@end

TransparentToolbar.m

#import "TransparentToolbar.h"

@implementation TransparentToolbar

-(void)insertSubview:(UIView *)view atIndex:(NSInteger)index
{
    //  This method is called with a view of class "UINavigationBarBackground" or "_UIToolbarBackground", respectively. It would be possible to check for this with NSStringFromClass([view class]) to be completely sure that we're skipping the right view.

    if (index != 0)
    {
        [super insertSubview:view atIndex:index];
    }
    else
    {
        // insert your custom background view, if you want to
    }
}


@end

EDIT: In iOS 5+, it's also possible to simply set the backgroundImage (which could be transparent). This is certainly the "cleaner" solution, but is less flexible than a custom UIView.

[someToolbar setBackgroundImage:[UIImage imageNamed:@"clear"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
Andreas Ley
  • 9,109
  • 1
  • 47
  • 57
1

This worked for me for iOS 6 and 7:

UIGraphicsBeginImageContextWithOptions(CGSizeMake(1, 1), NO, 0.0);
UIImage *blank = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

[self.toolBar setBackgroundImage:blank forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
david72
  • 7,151
  • 3
  • 37
  • 59