You must set the background color to pure in initWithFrame or / and initWithCoder
- (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code self.backgroundColor = [UIColor clearColor]; } return self; } - (id)initWithCoder:(NSCoder *)aDecoder { self = [super initWithCoder:aDecoder]; if (self) { self.backgroundColor = [UIColor clearColor]; } return self; }
the background of your control will be transparent by default, and then you can fill in any background color in drawRect if you want.
The reason this doesn't work in your example is because the control has a default black background that is set somewhere next to drawRect (possibly in the parent UIView init). When you set the color background, it comes in black. When you set clear, you will see a black background by default.
source share