Transparent UIToolBar

Sometimes it is really necessary to have a transparent UIToolBar within you iPhone app. Especially if you want to add a UIToolBar to your UINavigationBar you get some problems with overlaying backgrounds (UIBarStyleBlackOpaque did not work for a toolbar on a black opaque navigation bar).

A quick and pragmatic solution is to subclass UIToolBar and do some modifications. The following code is everything you need to get a really transparent tool bar:

@interface TransparentToolbar : UIToolbar
@end
 
@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

To apply a color to the including UIBarButtonItems you can set the bar style as usual.

That’s all for today,
Andreas

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章