使用backgroundColor时绘制部分透明的UIView

Pau*_*der 5 iphone uiview

我正在创建一个自定义的UIControl来呈现它的视图drawRect:.我想使用现有的backgroundColor属性来允许通过Interface Builder进行自定义.但是,如果设置了backgroundColor,则在调用drawRect:时,整个矩形已经被填充.​​我有opaque = NO和clearsContextBeforeDrawing = YES.如果我将backgroundColor设置为clearColor,那么我只能渲染我想要的部分.

如何防止UIView自动使用backgroundColor填充整个矩形?

我应该提到自定义控件放在Interface Builder中,因此initWithCoder:方法用于实例化对象.

这肯定是可能的,就好像你从UIButton继承 - 它使用backgroundColor - 矩形尚未填充.

fzw*_*zwo 1

由于这是一个自定义控件,您能否UIColor *myColor在 init 方法中添加一个变量 , and ,在从 nib 读取后,设置myColor = self.backgroundColor, and self.backgroundColor = [UIColor clearColor]?然后显然myColor在你的drawRect中使用。

我没试过这个,看起来有点难看,但也许它有效?