CALayer不呈现内容 [英] CALayer not rendering contents
问题描述
这一个是骗我。我有一个 CALayer
它创建在 UIViewController
的 viewDidLoad
方法类似这样:
This one is stumping me. I have a CALayer
which I create in a UIViewController
's viewDidLoad
method something like this:
- (void)viewDidLoad {
[super viewDidLoad];
UIImage* img = [UIImage imageNamed:@"foo.png"];
_imageLayer = [[CALayer alloc] init];
_imageLayer.bounds = CGRectMake( 0, 0, img.size.width, img.size.height);
_imageLayer.position = CGPointMake( self.view.bounds.size.width/2.0, self.view.bounds.size.height/2.0 );
_imageLayer.contents = (id)img.CGImage;
_imageLayer.contentsScale = img.scale;
[self.view.layer addSublayer:_imageLayer];
[_imageLayer setNeedsDisplay];
}
UIViewController
从一个nib加载并放置到在代码中创建的 UITabBarController
中。
The UIViewController
is loaded from a nib and placed into a UITabBarController
that was created in code.
我的问题是<当 UIViewController
变为可见时,c $ c> CALayer 不显示其 UIImage
我可以使 CALayer
渲染 UIImage
的唯一方法是将其内容设置为 UIViewController
可见之后,例如 viewDidAppear:
方法中的
My problem is that the CALayer
does not render its UIImage
contents when the UIViewController
becomes visible. The only way I can make the CALayer
to render the UIImage
is by setting its contents to the UIImage
after the UIViewController
is made visible, such as in the viewDidAppear:
method.
我真的更愿意做所有的设置在 viewDidLoad
方法?)。如何获取 CALayer
以呈现其内容?
I would really prefer to do all my set up in the viewDidLoad
method (isn't that what's it’s for?). How do I get the CALayer
to render its contents?
推荐答案
不需要在图层或超级视图上 setNeedsDisplay
。
You don't need that setNeedsDisplay
on the layer, or the superview.
setNeedsDisplay
告诉图层或视图它需要重绘其内容。 (对于视图,它将调用 drawRect:
来获取新内容;对于CALayer,它将询问其委托)。注意,这仅影响视图或图层本身,
setNeedsDisplay
tells a layer or view that it needs to redraw its contents. (For a view, it will call drawRect:
to get new contents; for a CALayer, it will ask its delegate.) Note that this affects only the view or layer itself, not its children.
由于您直接提供图层的内容,因此您不希望CA丢弃该内容并要求替换。
Since you are providing the content for the layer directly, you don't want CA to discard that content and ask for a replacement.
这篇关于CALayer不呈现内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!