ios - 我的自定义 UIView 如何绘制清晰的背景?

标签 ios objective-c uiview core-graphics

我需要制作自己的 UITableViewCellAccessoryDisclosureIndicator,这样我才能拥有自定义颜色。我需要将背景设置为清除。无论我尝试什么,我都无法让 clearColor 工作。当我将下面的 CGContextSetFillColorWithColor 行更改为 redColor 时,它是红色的。我试过设置不透明和 backgroundColor,然后调用 super drawRect 并删除我的填充代码并显示我的填充代码,但它仍然不好。我还以各种方式尝试了 CGContextClearRect。

这是模拟器中的红色版本。我需要 UIView 清晰可见,以便显示漂亮的背景图像。

我不太关心滚动性能,因为这个表的行数很少。

screenshot

- (instancetype) init {
    self = [super init];
    if (!self) return nil;

//    [self setOpaque:NO];
//    [self setBackgroundColor:[UIColor clearColor]];

    return self;
}

- (void)drawRect:(CGRect)rect {
//    [super drawRect:rect];

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);

    // my obviously silly/naive attempt at making the background clear
    CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor); // redColor works
    CGContextFillRect(context, rect);

    // drawing code for chevron
    CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
    CGContextSetLineWidth(context, 3.f);
    CGContextSetLineJoin(context, kCGLineJoinMiter);
    CGContextMoveToPoint(context, PADDING, PADDING);
    CGContextAddLineToPoint(context, self.frame.size.width - PADDING, self.frame.size.height/2);
    CGContextAddLineToPoint(context, PADDING, self.frame.size.height - PADDING);
    CGContextStrokePath(context);

    CGContextRestoreGState(context);
}

最佳答案

这个怎么样?

self.contentView.opaque = NO;
self.backgroundColor = [UIColor clearColor];

关于ios - 我的自定义 UIView 如何绘制清晰的背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24914085/

相关文章:

ios - 使用 MRProgress 更新进度

ios - 是否可以使用CoreLocation/MapKit查找纬度/经度附近的地址?

ios - 对 block 内弱引用的强引用

ios - UIView animateKeyframes 不起作用

ios - ARKit 1.5 如何获取垂直平面的旋转

ios - Apollo iOS swift 总是从本地缓存中获取 GraphQl

iOS 绘制带有底部边框的自定义 View

iphone - 有什么方法可以自定义 Sharekit 吗?

ios - 导航 PushViewController 不起作用

objective-c - @objc(ClassName) 用于 NSManagedObject 的子类