UIBezierPath简单矩形

fla*_*nka 7 core-graphics objective-c ios

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

- (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];
    }
}
Run Code Online (Sandbox Code Playgroud)

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

编辑:

下面的解决方案正在运行,但这也有效:

[self setOpaque:NO];
Run Code Online (Sandbox Code Playgroud)

Luk*_*cka 9

你绘图代码没问题.如果您希望自定义绘制视图具有透明背景,则只需设置即可

self.backgroundColor = [UIColor clearColor];
Run Code Online (Sandbox Code Playgroud)

在视图中 - (id)initWithFrame:(CGRect)frame

编辑:关于通话的一点注意事项[super drawRect:rect].UIViewdocs说:

如果UIView直接子类化,则此方法的实现不需要调用super.但是,如果要子类化其他视图类,则应在实现中的某个时刻调用super.