在iOS7上忽略UIImageView的backgroundColor?

时间:2013-09-26 20:00:26

标签: objective-c uiimageview ios7 uibackgroundcolor

在iOS7之前,像这样的代码在UIImageView的子类上运行良好。 但不再工作了。

-(id) initWithFrame:(CGRect) frame{
    self = [super initWithFrame:frame];
    if(self){
        self.backgroundColor = [UIColor redColor];
    }
    return self;
}

我也在InterfaceBuilder上检查过这个 而且,UIImageView的backgroundColor的值不会改变任何东西......
所有这些都适用于UIView ...
那么,它的UIImageView发生了什么?它还不是UIView的子类吗? 或者iOS7禁用此属性? 或者是否有任何新属性我必须设置一些东西来使其工作?

这段代码可能更好地描述了这个问题:

UIImageView *imageView =[ [UIImageView alloc] initWithImage:[UIImage imageNamed:@"ImageName.png"];
imageView.backgroundColor = [UIColor redColor];

更改backgroundColor的值时,如果没有图像属性,则会起作用。 但那时它不再是backgroundColor ......

2 个答案:

答案 0 :(得分:0)

如果您的UIImageView来自.xib / .storyboard,则会调用initWithCoder:而不是initWithFrame:。作为一个例子,我将这个小UIImageView子类放在一起,然后将一个带有居中内容的TestImageView放在一个故事板内的视图控制器视图中。故事板中的TestImageView背景颜色设置为红色。

@implementation TestImageView

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor blueColor];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.backgroundColor = [UIColor greenColor];
    }
    return self;
}

- (void)setBackgroundColor:(UIColor *)backgroundColor {
    NSLog(@"setting background color to %@", backgroundColor);
    [super setBackgroundColor:backgroundColor];
}

@end

运行应用程序创建了此输出:

setting background color to UIDeviceRGBColorSpace 1 0 0 1
setting background color to UIDeviceRGBColorSpace 0 1 0 1

这是在屏幕上:

enter image description here

如您所见,首先使用故事板中的红色调用setBackgroundColor(这发生在[super initWithCoder:aDecoder]中),然后使用initWithCoder中的绿色调用。

答案 1 :(得分:0)

只是为了更新。
正如Wain和Greg所建议的那样, 更改图像本身的不透明度解决了问题 只是不知道为什么在iOS7之前没关系,但事后很重要 我仍然想知道它可能只是iOS7本身的一个问题。无论如何,虽然我有近200张图像可供使用,但最好还是开始修改它们,而不仅仅是等待Apple的回答。
谢谢你的帮助!