When Subclassing the UIToolBar in an iPad App to make it transparent it ends up turning black
I'm trying to create a transparent UIToolBar in an iPad App by subclassing it. I have seen / read several examples on how to do this, and my code looks right, but when I run my App the background is black instead of transparent.
Here is my TransparentToolBar.h file:
#import <UIKit/UIKit.h>
@interface TransparentToolBar : UIToolbar {
}
@end
And now my .m file:
#import "TransparentToolBar.h"
@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;
}
- (id)initWithFrame:(CGRect)aRect {
if ((self = [super initWithFrame:aRect])) {
[self applyTranslucentBackground];
}
return self;
}
@end
It looks like the init functions aren't getting called, but the drawRect function is because its stripping the background that was set in IB.
When a view is unarchived from a XIB, the initializer that is invoked is -initWithCoder:
- (id)initWithCoder:(NSCoder *)coder
{
if (self = [super initWithCoder:coder])
{
[self applyTransluscentBackground];
}
return self;
}
精彩评论