透明背景UIView drawRect圈

use*_*418 25 objective-c uiview ios

我正在使用以下代码绘制一个白色笔划和属性指定的颜色的圆:

- (void)drawRect:(CGRect)rect {

    CGContextRef contextRef = UIGraphicsGetCurrentContext();

    CGContextClearRect(contextRef, rect);

    // Set the border width
    CGContextSetLineWidth(contextRef, 1.5);

    // Set the circle fill color to GREEN
    CGFloat red, green, blue, alpha;
    BOOL didConvert = [_routeColor getRed:&red green:&green blue:&blue alpha:&alpha];

    CGContextSetRGBFillColor(contextRef, red, green, blue, 1.0);

    // Set the cicle border color to BLUE
    CGContextSetRGBStrokeColor(contextRef, 255.0, 255.0, 255.0, 1.0);

    // Fill the circle with the fill color
    CGContextFillEllipseInRect(contextRef, rect);

    // Draw the circle border
    CGContextStrokeEllipseInRect(contextRef, rect);
}
Run Code Online (Sandbox Code Playgroud)

我回来的图像看起来像这样:

在此输入图像描述

我怎样才能删除黑色?

use*_*109 39

backgroundColor视图设置为[UIColor clearColor].

请注意,创建视图时,只需要设置一次背景颜色.

  • 要添加到这个答案 - 在自定义视图的`init`方法中设置背景颜色,而不是在`drawRect:`中. (8认同)

Nic*_*nux 17

我需要这个.当我设置这个,工作.

self.opaque = NO;
Run Code Online (Sandbox Code Playgroud)


小智 5

这在Swift 3上对我有用:

self.isOpaque = false
self.backgroundColor = UIColor.clear
Run Code Online (Sandbox Code Playgroud)