Search code examples
iosobjective-cswiftinterface-builderibdesignable

Clear IBDesignable views have black background


I'm attempting to create an IBDesignable class which can have a transparent background when rendered in Interface Builder. So far, I've gotten opaque black backgrounds. I've set up a simple demonstration of the issue:

I've placed a UIView in this scene in Interface Builder. You can't see it because its background is clear. This is what should the scene should look like.

A screenshot of an empty scene in interface builder

The only difference in this screenshot is that I've set the class of our previously invisible view to be 'DesignableView'.

A screen shot of an Interface Builder scene with a black box in the middle

Here is the entire implementation of the DesignableView class:

#import <UIKit/UIKit.h>

IB_DESIGNABLE
@interface DesignableView : UIView
@end

@implementation DesignableView
- (void)drawRect:(CGRect)rect {
    //rendering code goes here
}
@end

Is there a way to give the IBDesignable view the correct, transparent background?


Solution

  • I just remembered how I fix this. You need to override layoutSubviews

    - (void)layoutSubviews
    {
        [super layoutSubviews];
    
        self.backgroundColor = [UIColor clearColor];
    }