Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Couldn't UIToolBar be transparent?

Tags:

cocoa-touch

I try the following code, but it doesn't work.

[helloToolbar setBackgroundColor:[UIColor clearColor]];
like image 394
www.ruu.cc Avatar asked Mar 18 '10 09:03

www.ruu.cc


5 Answers

To make a completely transparent toolbar, use the method described here. In a nutshell, create a new TransparentToolbar class that inherits from UIToolbar, and use that in place of UIToolbar.

TransarentToolbar.h

@interface TransparentToolbar : UIToolbar
@end

TransarentToolbar.m

@implementation TransparentToolbar

// Override draw rect to avoid
// background coloring
- (void)drawRect:(CGRect)rect {
    // do nothing in here
}

// Set properties to make background
// translucent.
- (void) applyTranslucentBackground
{
    self.backgroundColor = [UIColor clearColor];
    self.opaque = NO;
    self.translucent = YES;
}

// Override init.
- (id) init
{
    self = [super init];
    [self applyTranslucentBackground];
    return self;
}

// Override initWithFrame.
- (id) initWithFrame:(CGRect) frame
{
    self = [super initWithFrame:frame];
    [self applyTranslucentBackground];
    return self;
}

@end

(code from the blog post linked above)

like image 77
morais Avatar answered Nov 10 '22 04:11

morais


In iOS 5, simply call setBackgroundImage and pass a transparent image.

Here's how I do it (I dynamically generate transparent image):

CGRect rect = CGRectMake(0, 0, 1, 1);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
CGContextFillRect(context, rect);
UIImage *transparentImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

[toolbar setBackgroundImage:transparentImage forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
like image 39
Nikolozi Avatar answered Nov 10 '22 04:11

Nikolozi


The best you can do is using

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

This will get you a black but translucent toolbar.

like image 41
Brandon Bodnar Avatar answered Nov 10 '22 02:11

Brandon Bodnar


Transparent (iOS 5.0):

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

Translucent:

[toolbar setBarStyle:UIBarStyleBlack];
[toolbar setTranslucent:YES];
like image 15
3lvis Avatar answered Nov 10 '22 04:11

3lvis


A cumulative solution for all devices, from oldest iOS 3.0 (iPhone 1) to newest iOS 6.1 (iPad mini).

@implementation UIToolbar (Extension)

- (void)drawRect:(CGRect)rect
{
    if (CGColorGetAlpha(self.backgroundColor.CGColor) > 0.f)
    {
        [super drawRect:rect];
    }
}

- (void)setTransparent
{
    //iOS3+
    self.backgroundColor = [UIColor clearColor];

    //iOS5+
    if ([self respondsToSelector:@selector(setBackgroundImage:forToolbarPosition:barMetrics:)])
    {
        [self setBackgroundImage:[[UIImage new] autorelease] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
    }
    //iOS6+
    if ([self respondsToSelector:@selector(setShadowImage:forToolbarPosition:)])
    {
        [self setShadowImage:[[UIImage new] autorelease] forToolbarPosition:UIToolbarPositionAny];
    }
}

@end

When you want a transparent toolbar, call setTransparent on it. When you want a non-transparent toolbar, set a backgroundColor of your choice or add an imageView by yourself.

like image 7
Cœur Avatar answered Nov 10 '22 03:11

Cœur