【问题标题】:UIView Background Color Always BlackUIView 背景颜色总是黑色
【发布时间】:2014-05-11 13:35:17
【问题描述】:

当我以编程方式将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;
}

为什么会这样?我需要改变什么?其实我希望背景是透明的。

【问题讨论】:

    标签: ios objective-c uiview


    【解决方案1】:

    当您实现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 完全或部分透明。

    【讨论】:

    • 当我使用这个命令时,由于某种原因,背景仍然是黑色的。解决方案对我不起作用。为什么会这样?
    • 我已经更正了我的答案。将视图的 opaque 属性设置为 NO 很重要。
    • 为什么它不再起作用了,随着我在绘制之前清除矩形,结果视图将有黑色背景,无论isOpaque设置为true还是false
    【解决方案2】:

    如果您使用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];
    }
    

    【讨论】:

    • 当我想要背景颜色时,答案非常有效。但我不能使用 [[UIColor clearColor] setFill];它仍然是黑色的。
    • @Pete,我看到的结果与您在 drawRect 中尝试将颜色设置为清除时看到的结果相同。要解决此问题,请删除该 UIRectFill 调用并在视图的 init 方法中将背景颜色设置为清除(哪个 init 方法适合您创建视图的方式)。
    • @Pete 请检查这个问题stackoverflow.com/questions/2125543/…。也许会有所帮助
    • @Pete 还要检查您视图的 opaque 属性是否为 NO。即your_view.opaque = NO.
    【解决方案3】:

    在 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
    

    【讨论】:

      【解决方案4】:
      backgroundViewController.providesPresentationContextTransitionStyle = YES;
      backgroundController.definesPresentationContext = YES;
      [overlayViewController setModalPresentationStyle:UIModalPresentationOverCurrentContext];
      

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

      【讨论】:

        猜你喜欢
        • 2017-05-30
        • 1970-01-01
        • 2013-12-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-08
        • 2013-11-02
        • 1970-01-01
        相关资源
        最近更新 更多