我的自定义视图不显示backgroundColor

时间:2017-03-18 08:53:38

标签: ios objective-c

我创建了一个视图,并设置了两个属性backgroundColorbackgroundAlpha,当我设置v.background = color时,backgroundColor为蓝色,但是当我设置v.background = [color colorWithAlphaComponent:b]时, backgroundColor是清晰的,好像b是零;

+ (instancetype)initWithBgrColor:(UIColor *)color bgrAlpha:(NSInteger) b
{
    view *v = [[view alloc] init];
    v.backgroundColor = [color colorWithAlphaComponent:b];
    return v;
} 

1 个答案:

答案 0 :(得分:0)

你一定不要看编译器的输出,看Xcode警告你参数类型。根据文档,colorWithAlphaComponent的alpha值参数是介于0.0和1.0之间的CGFloat,因此,您的NSInteger不正确。将您的代码更改为:

+ (instancetype)initWithBgrColor:(UIColor *)color bgrAlpha:(CGFloat) b
{
    NSLog(@"initWithBgrColor: b = %f", (float)b);
    view *v = [[view alloc] init];
    v.backgroundColor = [color colorWithAlphaComponent:b];
    return v;
} 

https://developer.apple.com/reference/uikit/uicolor?language=objc