【发布时间】:2023-03-11 01:28:02
【问题描述】:
我已经从图层中创建了一个自定义按钮。我在init 中创建了一堆图层。一切都按预期工作,直到我去修改*它们。只有在实际设备上才会出现问题,在模拟器上代码按预期工作。
我尝试通过将 needsDisplay 和 needsLayout 设置为 YES 来强制渲染图层。
-(void) setBackgroundColor:(UIColor *)backgroundColor
{
//return; // if I return here, the button gets initialized with it's default royal blue color. This works correctly on the simulator and device
[primaryBackground setColors: [NSArray arrayWithObject:(id)backgroundColor.CGColor]];
//return; //if I return here, primaryBackground will display a clear background on the device, but will display CGColor on the simulator.
CALayer* ll = [CALayer layer];
ll.frame=self.frame;
ll.cornerRadius=primaryBackground.cornerRadius;
[ll setBackgroundColor:backgroundColor.CGColor];
[primaryBackground addSublayer:ll];
//if I return here, primaryBackground will display the "expected" results on both platforms. IE: it dispalys the new layer, but not it's background color.
}
模拟器
设备
测试
我在 iOS 5.1 和 4.2 上进行了测试,结果相同。在模拟器版本 4.1、4.2 和 5.1 上似乎工作相同
【问题讨论】:
-
邮政编码或者它没有发生。
-
还有什么版本的iOS设备和iOS模拟器。
-
确保将图像导出为具有正常 RGB 配置文件的 .png。也许你有一个奇怪的颜色配置文件?!
-
当我浏览并缩减代码时,我能够解决这个问题。我认为石板灰色与背景不同,但这只是一种视觉错觉 - 它是相同的颜色。 BG 颜色只是在设备方面被“擦除”了。
-
您不应该将
backgroundColor.CGColor转换为(id)吗?(id)backgroundColor.CGColor。只是也许。
标签: iphone ios core-animation calayer cagradientlayer