CALayer not displaying

旧街凉风 提交于 2019-12-05 22:57:03

One problem (possibly the only problem) is that you're creating your color with all zero components. When you say 208/255, the compiler performs the division using integers and drops the remainder, so 208/255 is 0. You need to divide in floating point: 208.0f / 255.0f.

It's also much easier to use a UIColor instead of setting up the CGColorSpace and the CGColor yourself. Try this:

- (void)viewDidLoad {
    [super viewDidLoad];

    UIColor *reliantMagenta = [UIColor colorWithRed:208.0f / 255.0f
        green:27.0f / 255.0f blue:124.0f / 255.0f alpha:0.3f];

    CALayer *magentaLayer = [CALayer layer];
    magentaLayer.frame = CGRectMake(0, 0, 640, 960);
    magentaLayer.backgroundColor = reliantMagenta.CGColor;

    [self.view.layer addSublayer:magentaLayer];
}
rsswtmr

You're adding a layer as a sublayer of itself. [self layer] returns the view's existing layer. If you want to create a separate layer, you have to do it manually. There's probably something in the view system that keeps circular references from iterating out of control.

As a point of reference, you should make it a habit of calling [super viewDidLoad] before you do anything else. And an easier way of creating a CGColor is to create a UIColor and get its CGColor property.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!