ios - Subview's sublayers overlapping higher subviews -


i have issue: i'm creating uiview returned method, , part fine, i've noticed when add sublayers 1 of subviews, layers overlap subviews higher in hierarchy (textview , imageview) sublayers added testviewcopy appearing on top of these subviews, , aren't supposed to. have no idea what's going on here cause this.

code:

- (void)makeshareimages {     uiview *shareview = [self shareview];      uiview *testviewcopy = [shareview viewwithtag:0];      nsuinteger currentindex = 1;      (nsdictionary *sub in self.array)     {         nsarray *lastarray = [sub objectforkey:@"lastarray"];          (nsdictionary *dict in lastarray)         {             @autoreleasepool             {                 currentindex ++;                  circlelayer *layer = [[circlelayer alloc]init];                 layer.portrait = [[dict objectforkey:@"portrait"]boolvalue];                  layer.frame = testviewcopy.bounds;                  [testviewcopy.layer addsublayer:layer];                  nsdata *framedata = [self getsnapshotdatafromview:shareview];                  nsstring *savepath = [nsstring stringwithformat:@"%@/%lu.png",somepath,(unsigned long)currentindex];                  [framedata writetofile:savepath options:0 error:nil];             }         }     } }  - (uiview *)shareview {     uicolor *bgcolor = self.containerview.backgroundcolor;      cgsize size = self.containerview.bounds.size;      uiview *viewtoshare = [[uiview alloc]init];     viewtoshare.backgroundcolor = bgcolor;     viewtoshare.layer.cornerradius = 6.0;     viewtoshare.layer.maskstobounds = yes;      uiview *testviewcopy = [[uiview alloc]init];     testviewcopy.backgroundcolor = [uicolor clearcolor];     testviewcopy.contentmode = uiviewcontentmodescaleaspectfit;     testviewcopy.layer.maskstobounds = yes;     testviewcopy.tag = 0;      uitextview *textviewcopy = [[uitextview alloc]init];     textviewcopy.backgroundcolor = [uicolor clearcolor];     textviewcopy.tag = 1;     textviewcopy.textcontainerinset = self.textview.textcontainerinset;      uiimageview *profileimageviewcopy = [[uiimageview alloc]initwithframe:cgrectmake(2, 2, 32, 32)];     profileimageviewcopy.contentmode = uiviewcontentmodescaleaspectfit;     profileimageviewcopy.layer.maskstobounds = yes;     profileimageviewcopy.image = [self profileimage];     profileimageviewcopy.tag = 2;     profileimageviewcopy.layer.cornerradius = profileimageviewcopy.frame.size.width / 2.0;      viewtoshare.frame = cgrectmake(0, 0, size.width, size.height);     testviewcopy.frame = cgrectmake(0, 0, size.width, size.width);     textviewcopy.frame = cgrectmake(0, 0, size.width, size.height);      nsattributedstring *attributedstringcopy = [[nsattributedstring alloc]initwithattributedstring:self.textview.attributedtext];      textviewcopy.attributedtext = attributedstringcopy;      [viewtoshare addsubview:testviewcopy];     [viewtoshare addsubview:textviewcopy];     [viewtoshare addsubview:profileimageviewcopy];      return viewtoshare; }  - (nsdata *)getsnapshotdatafromview:(uiview *)view {     uigraphicsbeginimagecontextwithoptions(view.bounds.size, no, 0.0);     [view.layer renderincontext:uigraphicsgetcurrentcontext()];     uiimage *snapshot = uigraphicsgetimagefromcurrentimagecontext();     uigraphicsendimagecontext();      return uiimagepngrepresentation(snapshot); } 

your code behaving correctly. sibling views - subviews of common superview - have definite layering order, front. addsubview: adds subview last among siblings; thus, layered in front of existing subviews of same superview. if isn't want, insert subview @ further layer front, or add (at front) , move further in layering order.

moreover (and here think getting closer particular phenomenon have noticed), views layers. thus, layering order of views subset of layering order of layers. same thing said views equally true of layers, because views are layers: if add sublayer superlayer, added in front of other sublayers of superlayer, including subviews if superlayer view.

as write in current edition of book:

a view's subview's underlying layer sublayer of view's underlaying layer, other sublayers of view's underlying layer. therefore, can positioned anywhere among them in drawing order. fact view can interspersed among sublayers of superview's underlying layer surprising beginners.

enter image description here

it sounds you've discovered this, , appropriately surprised.

it may think of in terms of drawing of render tree, in active terms. in other words, instead of thinking how things look, think ios does. layer can have superlayer, except ultimate superlayer, , layer can have previous sibling, except first sublayer of superlayer. conversely, layer can have sublayers , can have next siblings. start drawing ultimate superlayer (the window). then, every layer have drawn, follow these 2 rules in order:

  • draw first sublayer, if any.

  • draw next sibling, if any.

every time draw layer, in front of everything have drawn. sublayers of superlayer appear in front of superlayer, , later siblings (and sublayers) appear in front of previous siblings (and sublayers). results in see on screen.

(however, note have more flexibility, because among siblings can manipulate drawing order without rearranging siblings, setting zposition property of layer.)


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -