Ответ 1
Установите backgroundColor
для представления на [UIColor clearColor]
.
Обратите внимание, что установка цвета фона требуется только один раз, когда создается представление.
Я рисую круг с белым штрихом и цветом, указанным свойством, используя этот код:
- (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);
}
Изображение, которое я возвращаю, выглядит следующим образом:
Как удалить черный?
Установите backgroundColor
для представления на [UIColor clearColor]
.
Обратите внимание, что установка цвета фона требуется только один раз, когда создается представление.
Мне это нужно. Когда я установил это, работал.
self.opaque = NO;
Это работало для меня на Swift 3:
self.isOpaque = false
self.backgroundColor = UIColor.clear
Вам нужно установить цвет фона для clearColor в init.
Вот пример
-(id)initWithCoder:(NSCoder*)aDecoder
{
self = [super initWithCoder:aDecoder];
if(self)
{
self.backgroundColor = [UIColor clearColor];
}
return self;
}
- (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);
}