简体   繁体   中英

How can my custom UIView draw a clear background?

I need to make my own UITableViewCellAccessoryDisclosureIndicator so I can have custom colors. I need to set the background to clear. No matter what I try, I can't get the clearColor to work. When I change the CGContextSetFillColorWithColor line below to redColor, it's red. I've tried setting opaque and backgroundColor and then calling super drawRect with my fill code removed and with my fill code present, but it's still no good. I've also tried CGContextClearRect in various ways.

Here's the red color version from the simulator. I need the UIView to be clear so that the pretty background image shows through.

I'm not terribly concerned with scrolling performance because this table will have very few rows.

截图

- (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);
}

How about this?

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

Designated. Flippin. Initializers. What a waste of time.

UIView's designated initializer is initWithFrame. initWithFrame doesn't call init. Notice that I overrided init. I had assumed that initWithFrame would call init.

Here's what works (notice I don't have to paint the background even though I'm not calling super.drawRect):

- (instancetype) initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (!self) return nil;

    // assume decent defaults
    [self setColor:[UIColor darkGrayColor]];
    [self setBackgroundColor:[UIColor clearColor]];

    return self;
}

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);

    CGContextSetStrokeColorWithColor(context, [self color].CGColor);
    CGContextSetLineWidth(context, 3.f);
    CGContextSetLineJoin(context, kCGLineJoinMiter);
    CGContextMoveToPoint(context, PADDING, PADDING);
    CGContextAddLineToPoint(context, rect.size.width - PADDING, rect.size.height/2);
    CGContextAddLineToPoint(context, PADDING, rect.size.height - PADDING);
    CGContextStrokePath(context);

    CGContextRestoreGState(context);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM