【问题标题】:passing argument 1 of 'setBackgroundColor:' from incompatible pointer type从不兼容的指针类型传递 'setBackgroundColor:' 的参数 1
【发布时间】:2011-07-21 06:15:46
【问题描述】:
- (void)drawRect:(CGRect)rect {
    // Drawing code.

    // BackgroundColor
    CALayer *myLayer = [self layer];    
    [myLayer setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5].CGColor];
    ...
}

当我想为图层设置背景颜色时,出现了警告。 但是代码运行正常。

================================================ =============

最佳答案是: #import <QuartzCore/QuartzCore.h>

【问题讨论】:

    标签: ios quartz-graphics layer


    【解决方案1】:

    如果您的模块中没有包含 Quartz,那么编译器将不知道 CGColor 作为一种类型并发出警告。

    #import <QuartzCore/QuartzCore.h> 放在您的标题中,它应该会删除警告。

    希望这会有所帮助,

    戴夫

    【讨论】:

    • 魔术:好球! @user754223:点击这个答案旁边的小检查。
    【解决方案2】:

    CALayerbackgroundColor 属性是CGColorRef 类型,这是UIColorCGColor 属性的返回类型,因此代码看起来不错。

    尝试这样做:

    [myLayer setBackgroundColor:[[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5] CGColor]];
    

    【讨论】:

      【解决方案3】:

      从命令中去掉CGColor转换,写成:

      [myLayer setBackgroundColor:(id)[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5].CGColor];
      

      抱歉...试试这个更新版本

      【讨论】:

        猜你喜欢
        • 2013-12-25
        • 2016-03-28
        • 1970-01-01
        • 2018-02-27
        • 1970-01-01
        • 2017-12-11
        • 2020-06-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多