我從人們可以在設置中配置的內容創建預覽。他們可以在設置中選擇一種顏色。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>"
)
編輯2: 嘗試更多一些,我注意到,在剛剛添加的CAGradientLayer並添加頂部後?也許這是問題?
(
"<CAGradientLayer: 0x170220100>",
"<CAGradientLayer: 0x170220e20>",
"<CAGradientLayer: 0x170029f40>",
"<CALayer: 0x17803d960>"
)
編輯3: 景觀層次,從而在屏幕截圖中強調了View
是一個與背景。
編輯4:
嘗試這樣做:但現在沒有一個CALayer的了:
[[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
因此,它似乎是因爲CALayer被刪除,UILabel給出了這個錯誤,因爲它沒有超級觀點。
你能解釋一下嗎?你是說你把'grad「的聲明移到了'setupCell'方法之外? – ravron
Ofcourse,嘗試編輯它好一點! – k3tzor
我會推薦使用一個屬性而不是一個實例變量(這就是你在這裏做的)。 – ravron