UIView背景颜色始终为黑色 [英] UIView Background Color Always Black
本文介绍了UIView背景颜色始终为黑色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我以编程方式向视图控制器中添加UIView
时,无法将背景颜色更改为任何其他颜色,它始终保持黑色.
When I add a UIView
to a View Controller programmatically I am not able to change the background color to any other color, it always remains black.
- (void)drawRect:(CGRect)rect
{
// Drawing code
self.backgroundColor = [UIColor blueColor];
UIBezierPath *path = [[UIBezierPath alloc] init];
[path moveToPoint:CGPointMake(100, 33)];
[path addLineToPoint:CGPointMake(200, 33)];
path.lineWidth = 5;
[[UIColor redColor] setStroke];
[path stroke];
}
当我注释掉drawrect:
并将self.backgroundColor = [UIColor blueColor];
添加到初始化程序时,颜色会发生变化:
When I comment drawrect:
out and add self.backgroundColor = [UIColor blueColor];
to the initializer the color changes:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.backgroundColor = [UIColor blueColor]
}
return self;
}
这是为什么,我需要更改什么?实际上,我希望背景是透明的.
Why is this and what do I need to change? Actually I would like the background to be transparent.
推荐答案
backgroundColor
将被忽略.将您的代码更改为
backgroundColor
of your view is ignored if you draw view yourself using drawRect
. Change your code to
- (void)drawRect:(CGRect)rect
{
// Drawing code
[[UIColor blueColor] setFill]; // changes are here
UIRectFill(rect); // and here
UIBezierPath *path = [[UIBezierPath alloc] init];
[path moveToPoint:CGPointMake(100, 33)];
[path addLineToPoint:CGPointMake(200, 33)];
path.lineWidth = 5;
[[UIColor redColor] setStroke];
[path stroke];
}
这篇关于UIView背景颜色始终为黑色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文