您在哪里设置子视图的图层属性?为什么不在initWithCoder中 [英] Where do you set a subview's layer properties? Why not in the initWithCoder

查看:96
本文介绍了您在哪里设置子视图的图层属性?为什么不在initWithCoder中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义 UITableViewCell (对于本示例,我们假设子类为 MyViewCell ),与之关联的Nib文件 MyViewCell.xib 。笔尖包含一个 UITableViewCell 和一个子视图 UIView (名为 cardContainer )就是一个带有蓝色背景的矩形。我想在 UIView 周围添加一个阴影,因此我在 -initWithCoder 调用中添加了设置图层属性:

  @implementation MyViewCell 


-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if(self){
[self initView];
}
返回自我;
}


-(无效)initView
{
UIBezier Path * shadowPath = [UIBezierPath bezierPathWithRect:view.bounds];
[self.cardContainer.layer setShadowColor:[UIColor blackColor] .CGColor];
[self.cardContainer.layer setShadowOpacity:0.8];
[self.cardContainer.layer setShadowRadius:3.0];
[self.cardContainer.layer setShadowOffset:CGSizeMake(2.0,2.0)];
view.layer.shadowPath = shadowPath.CGPath;
}


@end

问题我遇到的是这些图层属性未绘制。如果我在 awakeFromNib drawRect 内调用 -initView 它按预期绘制。我的问题:为什么我的原始代码不起作用?我应该在哪里调用 initView ?有一些视图生命周期吗?我知道 initWithCoder 没有连接插座,但是在运行时没有崩溃。



我阅读了


重要提示:由于无法保证从归档实例化对象的顺序,因此初始化方法不应发送mes圣人到层次结构中的其他对象。可以从awakeFromNib内部安全地发送到其他对象的消息,这时可以确保所有对象都未归档和初始化(尽管不一定会唤醒)。



I created a custom UITableViewCell (for this example, let's just say the subclass is MyViewCell) which has an Nib file associated to it MyViewCell.xib. The nib contains a UITableViewCell with one subview UIView (named cardContainer) that's simply a rectangle with a blue background. I want to add a drop shadow around the UIView, so I added set the layer properties in the -initWithCoder call:

@implementation MyViewCell


- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self initView];
    }
    return self;
}


- (void) initView
{
    UIBezier Path*shadowPath =[UIBezierPath bezierPathWithRect:view.bounds];        
    [self.cardContainer.layer setShadowColor: [UIColor blackColor].CGColor];
    [self.cardContainer.layer setShadowOpacity: 0.8];
    [self.cardContainer.layer setShadowRadius:3.0];
    [self.cardContainer.layer setShadowOffset: CGSizeMake(2.0,2.0)];
    view.layer.shadowPath = shadowPath.CGPath;
}


@end

The problem I'm having is that these layer properties aren't being drawn. If I call the -initView call within awakeFromNib or drawRect it's drawn as expected. My question: why doesn't my original code work? Where should I be calling initView? Is there some view lifecycle? I understand that the initWithCoder doesn't have the outlets connected, but it didn't crash at runtime.

I read through Apple documentation around Views and searched through the SO questions without finding an answer. I found this SO answer, but again doesn't explain.

解决方案

Hey I found a better way to do this ,just add some runtime attributes for your subview cardContainer

like this

no more code in .m file anymore.

EDIT:

From:NSNibAwaking Protocol

Important: Because the order in which objects are instantiated from an archive is not guaranteed, your initialization methods should not send messages to other objects in the hierarchy. Messages to other objects can be sent safely from within awakeFromNib—by which time it’s assured that all the objects are unarchived and initialized (though not necessarily awakened, of course).

这篇关于您在哪里设置子视图的图层属性?为什么不在initWithCoder中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆