UIView 填充颜色
Posted
技术标签:
【中文标题】UIView 填充颜色【英文标题】:UIView Fill color 【发布时间】:2011-03-25 05:45:05 【问题描述】:我在 nib 文件中添加了一个UIView
。我想用drawRect
方法中的颜色填充UIView
。我该怎么做?
【问题讨论】:
【参考方案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】:在表格视图单元格的子视图中设置背景颜色会导致该视图不可见,因为启用编辑样式时,选择一个单元格会使所有背景视图颜色发生变化
【讨论】:
以上是关于UIView 填充颜色的主要内容,如果未能解决你的问题,请参考以下文章