ios – CALayers永远不会调用它的委托的drawLayer:inContext:甚至在[layer setNeedsDisplay]和[layer display]之后

我无法弄清楚为什么我正在创建的CALayer没有调用他们的drawLayer方法.我为它们创建了一个drawLayer委托对象,但它永远不会被调用.从UIView子类:
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

       UIView *bg = [[UIView alloc] initWithFrame:self.bounds];
       // I have a property called 'bg' to reference the child UIView
       self.bg = bg;
       [self addSubview:self.bg];

       // an NSObject subclass I use as a CALayer delegate
       MyLayerDelegate *drawer = [MyLayerDelegate new];
       self.drawer = drawer;


       CAGradientLayer *layer = [CAGradientLayer layer];;
       layer.delegate = self.drawer;
       layer.name = @"bg";

       [self.bg.layer addSublayer:layer];

       [layer setNeedsdisplay];
       [layer display];

    }
    return self;
}

在self.drawer我有

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context
{
    NSLog(@"layer called drawLayer");
    // Some drawing goes here
}

我尝试了很多不同的东西.我已经尝试在self.bg.layer上设置setNeedsdisplay,我在self.bg上尝试了setNeedsdisplay,我在addSublayer之前和之后尝试了[layer setNeedsdisplay],我在addSublayer之前和之后尝试了[图层显示].我曾尝试使用和不使用[图层显示]和[图层displayIfNeeded].

为什么图层拒绝绘制?

解决方法

图层框架必须为非零.这解决了这个问题:
layer.frame = self.bg.bounds;
[layer setNeedsdisplay];
[self.bg.layer addSublayer:layer];

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...