UIView渐变在第二次保存时不会改变

时间:2014-07-08 10:06:59

标签: ios objective-c uiview

我正在根据人们可以在设置中配置的内容创建预览。他们可以在设置中选择颜色。

我的视图层次结构:

  • 设置(选择树的颜色和预览树)
  • 选择树的颜色(此处用户可以选择树的颜色,使用保存按钮)

我的保存按钮unwindSegue,调用setupCell方法。 但是我的UIView的背景渐变层不想改变。

代码:

- (IBAction)setupCellSegue:(UIStoryboardSegue *)segue{
    [self setupCell];
}

-(void)setupCell{
    //Corners
    self.Preview.layer.cornerRadius = 25;
    self.Preview.layer.masksToBounds = YES;

    //Background
    self.Preview.backgroundColor = [UIColor clearColor];

    CAGradientLayer *grad = [CAGradientLayer layer];

    grad.frame = self.Preview.bounds;
    grad.colors = [[[Kleuren sharedInstance] kleurenArray] objectForKey:[standardDefaults objectForKey:@"Kleur"]];

    [self.Preview.layer insertSublayer:grad atIndex:0];

    //Text
    self.optionOutlet.textColor = [Kleuren.sharedInstance.tekstKleurenArray objectForKey:[standardDefaults objectForKey:@"TekstKleur"]];
}

修改 以下是视图中我的子图层中的NSLog。如何用新的CAGradientLayer替换旧的[self.Preview.layer replaceSublayer:0 with:grad]; ( "<CAGradientLayer: 0xcce9eb0>", "<CALayer: 0xccef760>" ) ?试过这个:

(
    "<CAGradientLayer: 0x170220100>",
    "<CAGradientLayer: 0x170220e20>",
    "<CAGradientLayer: 0x170029f40>",
    "<CALayer: 0x17803d960>"
)

编辑2: 在尝试了一些之后,我注意到CAGradientLayer刚刚添加并添加到顶部?也许这就是问题?

View

编辑3: 查看层次结构,因此屏幕截图中突出显示的[[self.Preview layer] replaceSublayer:[[[self.Preview layer] sublayers] objectAtIndex:0] with:grad]; 是具有背景的2014-07-08 18:57:18.365 ///[3324:60b] View hierarchy unprepared for constraint. Constraint: <NSIBPrototypingLayoutConstraint:0xe88e0b0 'IB auto generated at build time for view with fixed frame' UILabel:0xe88dae0.left == UIView:0xe88da50.left + 20> Container hierarchy: <UIView: 0xe88da50; frame = (59 54; 202 202); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0xe88dab0>> | <CAGradientLayer: 0xe88cd50> (layer) View not found in container hierarchy: <UILabel: 0xe88dae0; frame = (20 20; 162 162); text = 'Option'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0xe88db90>> That view's superview: NO SUPERVIEW View

编辑4:

试过这个:但现在它再也没有CALayer了:

{{1}}

所以我会收到以下错误:

{{1}}

所以似乎是因为删除了CALayer,UILabel会出现此错误,因为它没有superview。

3 个答案:

答案 0 :(得分:0)

尝试添加

 [self.Preview.layer setNeedsDisplay];
在setupCell之后

。这应该重绘图层

答案 1 :(得分:0)

不能立即确定您的问题是什么。

首先突然发现,在你的第一个编辑中,你误用了replaceSublayer:with:。请参阅the documentation for that method for details,但基本上在需要传递CALayer时使用整数文字 - 我很惊讶编译器没有抱怨。该方法的声明是:

- (void)replaceSublayer:(CALayer *)oldLayer with:(CALayer *)newLayer

要正确使用此方法,您需要保留对渐变图层的引用。也许创建一个名为grad的新属性并将其存储在那里。稍后,当您要替换它时,请将self.grad作为replaceSublayer:with:传递到oldLayer

修好后,有以下几点需要检查:

  • 您认为setupCell是否会被调用?在那里设置一个断点来检查。

  • 您确定在重新计算渐变图层时,颜色是否已更改?每次通过grad.colors时,请使用断点检查setupCell的值。

答案 2 :(得分:0)

找到它,我已将CAGradientLayer *grad;放在{}

之间的实施声明中

将声明放在我的班级实施中。我可以重新使用声明,而不是每次都通过将它放入setupCell来创建一个新实例。

@implementation class {
   CAGradientLayer *grad
}

-(void)setupCell{
   // Do stuff here with the gradient layer.
}

我不知道这是正确的解释,但它有效。是我最后出现的事情!