UIView背景颜色始终为黑色

25

当我程序化地将 UIView 添加到视图控制器中时,我无法将其背景颜色更改为任何其他颜色,它始终保持为黑色。

- (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];时,颜色会更改:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor = [UIColor blueColor]
    }
    return self;
}

为什么会这样,我需要做出什么改变呢?实际上,我希望背景是透明的。

4个回答

36

由于您正在实现drawRect:,这将覆盖UIView的任何默认绘制。

在呈现路径之前,您必须将自定义视图的opaque属性设置为NO并清除上下文:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.opaque = NO;
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    // Drawing code
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextClearRect(context, rect);

    UIBezierPath *path = [[UIBezierPath alloc] init];
    [path moveToPoint:CGPointMake(100, 33)];
    [path addLineToPoint:CGPointMake(200, 33)];
    path.lineWidth = 5;
    [[UIColor redColor] setStroke];
    [path stroke];
}

https://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/uiview/uiview.html#//apple_ref/occ/instp/UIView/opaque

不透明视图应该使用完全不透明的内容填充其边界,也就是说,内容的alpha值应为1.0。如果视图是不透明的,但未填满其边界或包含完全或部分透明的内容,则结果不可预测。如果视图是完全或部分透明的,您应该始终将此属性的值设置为NO。


1
当我使用这个命令时,背景出现黑屏的情况。解决方案对我不起作用。这是为什么呢? - Pete
1
我已经更正了我的答案。将视图的opaque属性设置为NO非常重要。 - iOSX
为什么它不再起作用了,只要在绘制之前清除矩形,结果视图就会有黑色背景,无论 isOpaque 设置为 true 还是 false - K.F
它对我有效。谢谢。 - undefined

20

如果您使用drawRect自己绘制视图,则会忽略视图的backgroundColor。请将代码更改为:

- (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];
}

4
当我想要设置背景颜色时,答案的效果非常完美。但是我无法使用[[UIColor clearColor] setFill];它仍然显示为黑色。 - Pete
7
@Pete,当尝试将颜色设置为clear时,在drawRect中我看到了与你一样的结果。为了解决这个问题,请删除UIRectFill调用,并在视图的init方法中将背景颜色设置为clear(根据您创建视图的方式选择相应的init方法)。 - rdelmar
3
@Pete,请查看这个问题https://dev59.com/unI95IYBdhLWcg3w2Rzz。也许它会有所帮助。 - Avt
7
请检查您的视图opaque属性是否为NO,即your_view.opaque = NO - Avt

2

在initWithFrame:方法中设置背景颜色,然后在drawRect:方法中进行自定义绘制。这应该可以解决您的问题。drawRect:方法是为了进行自定义绘制而不是设置视图特定属性而实现的。

@implementation MyView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
      self.backgroundColor = [UIColor yellowColor];
    }
    return self;
}
- (void)drawRect:(CGRect)rect{
    UIBezierPath *path = [[UIBezierPath alloc] init];
    [path moveToPoint:CGPointMake(100, 33)];
    [path addLineToPoint:CGPointMake(200, 33)];
    path.lineWidth = 5;
    [[UIColor redColor] setStroke];
    [path stroke];

}

@end

这是对我有效的 Swift 5 解决方案。 - devjme

1
backgroundViewController.providesPresentationContextTransitionStyle = YES;
backgroundController.definesPresentationContext = YES;
[overlayViewController setModalPresentationStyle:UIModalPresentationOverCurrentContext];

或者

enter image description here

我苦苦挣扎了很多个小时。如果这是一个模态跳转,将over current context设置为属性。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接