简体   繁体   English

UIView背景颜色总是黑色

[英]UIView Background Color Always Black

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. 当我以编程方式将UIView添加到View Controller时,我无法将背景颜色更改为任何其他颜色,它始终保持黑色。

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

When I comment drawrect: out and add self.backgroundColor = [UIColor blueColor]; 当我评论drawrect: out并添加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. 其实我希望背景是透明的。

As you are implementing drawRect: this will override any default drawing of the UIView . 在实现drawRect:这将覆盖UIView任何默认绘图。

You must set the opaque property of your custom view to NO and clear the context before you render the path: 必须将自定义视图的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 https://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/uiview/uiview.html#//apple_ref/occ/instp/UIView/opaque

An opaque view is expected to fill its bounds with entirely opaque content—that is, the content should have an alpha value of 1.0. 预计不透明视图将使用完全不透明的内容填充其边界 - 也就是说,内容的alpha值应为1.0。 If the view is opaque and either does not fill its bounds or contains wholly or partially transparent content, the results are unpredictable. 如果视图不透明并且未填充其边界或包含完全或部分透明的内容,则结果是不可预测的。 You should always set the value of this property to NO if the view is fully or partially transparent. 如果视图完全透明或部分透明,则应始终将此属性的值设置为NO。

backgroundColor of your view is ignored if you draw view yourself using drawRect . 如果使用drawRect自己绘制视图,则会忽略视图的backgroundColor 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];
}

Set the background color in initWithFrame: method and do your custom drawing inside drawRect:. 在initWithFrame:方法中设置背景颜色,并在drawRect:中进行自定义绘制。 That should be able to solve your problem. 那应该能够解决你的问题。 The drawRect: is implemented in order to do custom drawing not to set view specific properties. 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
backgroundViewController.providesPresentationContextTransitionStyle = YES;
backgroundController.definesPresentationContext = YES;
[overlayViewController setModalPresentationStyle:UIModalPresentationOverCurrentContext];

or 要么

在此输入图像描述

I struggled for many hours . 我挣扎了好几个小时。 If its a model segue, set over current context as property. 如果它是模型segue,则将当前上下文设置为属性。 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM