Clear IBDesignable images have a black background

I am trying to create an IBDesignable class that can have a transparent background when rendering in Interface Builder. So far, I have had opaque black backgrounds. I installed a simple demo of the problem:

I placed the UIView in this scene in Interface Builder. You cannot see it because its background is clear. Here's what the scene should look like.

A screenshot of an empty scene in interface builder

The only difference in this screenshot is that I 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 IBDesignable a view of the correct transparent background?

+7
ios objective-c swift interface-builder ibdesignable
source share
1 answer

I just remembered how I fix it. You need to override layoutSubviews

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

All Articles