7

我只想使用以下函数为视图绘制一个简单的矩形:

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    if (self.drawTextBouble) {
        [[UIColor blueColor] setFill];
        UIBezierPath *aPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(40, 0, 230, 120) cornerRadius:12.0];
        [aPath fill];
    }
}

上面的代码用纯黑色背景填充视图,矩形外不透明。我怎样才能解决这个问题?

编辑:

下面的解决方案有效,但这也有效:

[self setOpaque:NO];
4

1 回答 1

9

You drawing code is OK. If you want the custom drawn view to have transparent background, you just need to set

self.backgroundColor = [UIColor clearColor];

in view's - (id)initWithFrame:(CGRect)frame

Edit: Just a little note regarding calling [super drawRect:rect]. UIView docs says:

If you subclass UIView directly, your implementation of this method does not need to call super. However, if you are subclassing a different view class, you should call super at some point in your implementation.

于 2013-06-18T07:18:15.203 回答