UIView背景颜色总是黑色

时间:2014-05-11 13:35:17

标签: ios objective-c uiview

当我以编程方式向视图控制器添加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 个答案:

答案 0 :(得分:26)

在您实施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 :(得分:17)

如果您使用backgroundColor自己绘制视图,则会忽略您的视图的

drawRect。将您的代码更改为

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

答案 2 :(得分:1)

在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

答案 3 :(得分:1)

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

enter image description here

我挣扎了好几个小时。如果它是模型segue,则将当前上下文设置为属性。 !

相关问题