【问题标题】:UIView Fill colorUIView 填充颜色
【发布时间】:2011-03-25 05:45:05
【问题描述】:

我在 nib 文件中添加了一个UIView。我想用drawRect 方法中的颜色填充UIView。我该怎么做?

【问题讨论】:

    标签: ios iphone uiview fill drawrect


    【解决方案1】:

    您可以使用 UIView 的frame 属性来绘制矩形。只需将其传递给CGContextFillRect 函数(已设置上下文填充颜色)。

    使用当前图形状态下的填充颜色绘制包含在提供的矩形内的区域。

    所以drawRect中的代码:

    - (void)drawRect:(CGRect)rect 
    {
        CGContextRef context = UIGraphicsGetCurrentContext ();
    
        // The color to fill the rectangle (in this case black)
        CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, 1.0);
    
        // draw the filled rectangle
        CGContextFillRect (context, self.bounds);
    }
    

    【讨论】:

    • 如何使用frame属性绘制矩形?
    • @Priya frame 属性是一个 CGRect (视图的矩形)。这是您传递给 CGContextFillRect 的参数。我列出的代码将满足您的需求。
    • 在 CGContextFillRect 中使用 self.bounds。圆角:self.layer.cornerRadius = self.height / 2; self.clipsToBounds = YES
    【解决方案2】:

    为什么不将 backgroundColor 设置为您想要的颜色填充?

    【讨论】:

      【解决方案3】:

      如果你真的需要使用drawRect:,你可以使用这个:

      - (void)drawRect:(CGRect)rect {
          CGContextSetStrokeColorWithColor(context, [[UIColor blackColor] CGColor]);
          CGContextSetFillColorWithColor(context, [[UIColor blackColor] CGColor]);
          CGContextAddRect(context, CGRectMake(0, 0, 320, 460));
      }
      

      【讨论】:

        【解决方案4】:

        在表格视图单元格的子视图中设置背景颜色会导致该视图不可见,因为启用编辑样式时,选择一个单元格会使所有背景视图颜色发生变化

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-01-23
          • 1970-01-01
          • 2011-10-29
          • 2012-04-24
          • 2014-10-27
          • 2018-12-12
          • 2021-06-17
          • 1970-01-01
          相关资源
          最近更新 更多