Transparent UIToolbar
Asked Answered
F

7

9

I wrote the following code to make my toolbar transparent.

[mtoolbar setBackgroundColor:[UIColor clearColor]];

How do I make UIToolbar transparent?

Frenulum answered 6/6, 2011 at 10:4 Comment(2)
Do you want a truly transparent toolbar, or just a translucent one?Conjunctivitis
possible duplicate of How to draw a transparent UIToolbar or UINavigationBar in iOS7Temporal
L
12

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

Levantine answered 6/6, 2011 at 10:8 Comment(5)
I think we need to set the property yesFrenulum
Edited :) Was just a quick guess, I've not worked directly with the toolbar before.Levantine
Its not working for me. The UIToolbar's background color is in black transparent now. But I need it in clear color.Emmettemmey
See the comment that matches David H's answer.Levantine
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 https://mcmap.net/q/176481/-visually-modifying-a-uitoolbar-from-xcode-storyboard).Pecan
G
11
[self.toolbar setBackgroundImage:[UIImage new]
              forToolbarPosition:UIToolbarPositionAny
                      barMetrics:UIBarMetricsDefault];

[self.toolbar setBackgroundColor:[UIColor clearColor]];
Goddart answered 16/4, 2013 at 11:25 Comment(0)
H
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];
Houri answered 13/12, 2012 at 5:51 Comment(0)
R
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

Rectum answered 6/6, 2011 at 10:12 Comment(0)
J
2
for (UIView * sv in [toolBar subviews])
{
     [sv removeFromSuperview];
}

;) any iOs

Jeanelle answered 26/10, 2012 at 8:27 Comment(0)
B
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];
Boxthorn answered 21/8, 2012 at 15:53 Comment(0)
H
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];
Hepato answered 7/4, 2014 at 17:38 Comment(1)
There is still a border shown. Is there a fix for it ?Unaneled

© 2022 - 2024 — McMap. All rights reserved.