使UIView透明化

pet*_*234 16 screen transparent uiview ios

我想在我的UIViewController中添加一个UIView,但我希望它是透明的,直到我决定在UIView中创建一个Oval.

在我创建椭圆之前,我可以将视图的alpha设置为0但是当我想添加椭圆时,背景颜色仍然是黑色.

这是一堆椭圆形的样子

图片

在我的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];
}
Run Code Online (Sandbox Code Playgroud)

Dha*_*ngh 25

请尝试使用这个

view.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.0];
view.opaque = NO;
Run Code Online (Sandbox Code Playgroud)

  • 使用“UIColor.clear”将是标准化整个地方的代码的更好选择。 (2认同)

Guo*_*uan 16

- (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];
}
Run Code Online (Sandbox Code Playgroud)


Nat*_*sha 5

在你的ViewController中,你可以说opaque是NO.比方说,您的视图名为myCustomView.

- (void)viewDidLoad {
    [super viewDidLoad];
    self.myCustomView.opaque = NO;   
}
Run Code Online (Sandbox Code Playgroud)

或者您可以转到故事板并取消选中不透明的复选框,例如 - 在此输入图像描述