ios - Sublayer only draws when created in initWithFrame, not initWithCoder -


i have custom view sublayer (cashapelayer) , subview (uilabel). when create layer in initwithcoder , set background color, shows black. however, if move code initwithframe, color shows successfully.

are not supposed create sublayers in initwithcoder?

here way can code work:

- (id)initwithframe:(cgrect)frame {     self = [super initwithframe:frame];     if (self) {         self.colorlayer = [cashapelayer layer];         self.colorlayer.opacity = 1.0;         [self.layer addsublayer:self.colorlayer];     }     return self; }  - (id)initwithcoder:(nscoder *)adecoder {     self = [super initwithcoder:adecoder];     if (self) {          self.textlabel = [[uilabel alloc] initwithframe:self.bounds];         self.textlabel.font = [uifont primaryboldfontwithsize:12];         self.textlabel.textcolor = [uicolor whitecolor];         self.textlabel.textalignment = nstextalignmentcenter;         self.textlabel.backgroundcolor = [uicolor clearcolor];         [self addsubview:self.textlabel];     }      return self;  }  - (void)drawrect:(cgrect)rect {     //custom drawing of sublayer } 

update:

turns out in drawrect setting fill color wrong. should have used colorlayer.fillcolor = mycolor.cgcolor instead of [mycolor setfill] [path fill]

the difference between initwithframe: , initwithcoder: initwithcoder: called when view created storyboard/nib.

if add programatically, example:

uiview *v = [[uiview alloc] initwithframe:...]; [self.view addsubview:v]; 

initwithframe: called.

the idea create base init method , call in both init. in way initialisation sets of properties in both scenario, when view added programatically or in storyboard.

for example:

-(void)baseinit {     self.colorlayer = [cashapelayer layer];     self.colorlayer.opacity = 1.0;     //... other initialisation }  - (id)initwithframe:(cgrect)frame {     self = [super initwithframe:frame];     if (self) {         [self baseinit];     }     return self; }  - (id)initwithcoder:(nscoder *)adecoder {     self = [super initwithcoder:adecoder];     if (self) {          [self baseinit];     }      return self; } 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -