I want to add a UIView to my UIViewController but I want it to be transparent until I decide to create an Oval inside the UIView.
Until I create the oval, I can set my view's alpha to 0 but when I want to add the oval, the background color is still black.
here's what it looks like with a bunch of ovals
In my UIView:
- (void)drawRect:(CGRect)rect
{
[self pushContext];
UIBezierPath *oval = [UIBezierPath bezierPathWithOvalInRect:self.bounds];
[[UIColor redColor] setFill];
[oval fill];
self.opaque = NO;
[self setBackgroundColor:[UIColor clearColor]];
[oval addClip];
[self setBackgroundColor:[UIColor clearColor]];
[self popContext];
}
Any SwiftUI view can be partially or wholly transparent using the opacity() modifier. This accepts a value between 0 (completely invisible) and 1 (fully opaque), just like the alpha property of UIView in UIKit.
xib, you simply do it in interface builder by selecting the option "Clear Color" for the Background of the view in the Utilities Pane (the pane on the right). "Clear Color" will give the view a completely transparent background.
Please try to use this one
view.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.0];
view.opaque = NO;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.opaque = NO;
}
return self;
}
- (void)drawRect:(CGRect)rect
{
[[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.0] setFill];
UIRectFill(rect);
[self pushContext];
UIBezierPath *oval = [UIBezierPath bezierPathWithOvalInRect:self.bounds];
[[UIColor redColor] setFill];
[oval fill];
[oval addClip];
[self popContext];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With