【问题标题】:Draw partially transparent UIView while using the backgroundColor使用 backgroundColor 绘制部分透明的 UIView
【发布时间】:2010-09-23 05:55:56
【问题描述】:

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

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

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

这当然应该是可能的,就好像你从 UIButton 继承 - 它使用背景颜色 - 矩形尚未被填充。

【问题讨论】:

    标签: iphone uiview


    【解决方案1】:

    你需要覆盖:

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

    这是应用背景颜色的地方。如果想让view处理背景色,调用super的drawLayer方法。

    【讨论】:

      【解决方案2】:

      既然这是一个自定义控件,是否可以添加一个变量UIColor *myColor,并在init方法中,在从nib读取它之后,设置myColor = self.backgroundColorself.backgroundColor = [UIColor clearColor]?然后显然在你的drawRect中使用myColor

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

      【讨论】:

        猜你喜欢
        • 2015-02-17
        • 1970-01-01
        • 1970-01-01
        • 2013-07-20
        • 2011-03-14
        • 2021-09-05
        • 2015-01-26
        • 1970-01-01
        • 2019-09-10
        相关资源
        最近更新 更多