代码之家  ›  专栏  ›  技术社区  ›  Paul Alexander

使用backgroundColor绘制部分透明的UIView

  •  5
  • Paul Alexander  · 技术社区  · 15 年前

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

    如何防止UIView用背景色自动填充整个矩形?

    我应该提到,自定义控件被放置在接口生成器中,因此initWithCoder:方法被用来实例化对象。

    这当然是可能的,就像你从UIButton继承的一样-它使用了backgroundColor-矩形没有被填充。

    2 回复  |  直到 14 年前
        1
  •  2
  •   YuzaKen    14 年前

    你需要超越:

    - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx;
    

        2
  •  1
  •   fzwo    14 年前

    既然这是一个自定义控件,你能添加一个变量吗 UIColor *myColor ,并且,在init方法中,从nib读取它之后,设置 myColor = self.backgroundColor self.backgroundColor = [UIColor clearColor] ? 那么显然要用 myColor 在你的抽屉里。

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