ios - Swift 3 - How to remove all Subviews from Superview as well as Array -
i have double for-loop creates x y grid of uiview cgrect squares. loop adds each uiview/square of grid 2d array allowing me access each cell of grid , alter color/positioning index values of loop.
the loop seems work fine , displays cells/squares perfectly, after while want remove of squares , empty array (but not entirely delete) make room new next grid (which may of different size). created function remove views superview.
this how creating each "cell" of grid , placing each 2d array:
let xstart = int(size.width/2/2) - ((gridsizex * gridscale)/2) let ystart = int(size.height/2/2) - ((gridsizey * gridscale)/2) let cell : uiview! cell = uiview(frame: cgrect(x: xstart + (xpos * gridscale), y:ystart + (ypos * gridscale), width:gridscale, height:gridscale)) cell.layer.borderwidth = 1 cell.layer.bordercolor = uicolor(red:0.00, green:0.00, blue:0.00, alpha:0.02).cgcolor cell.backgroundcolor = uicolor(red:1.00, green:1.00, blue:1.00, alpha:0) cell.tag = 100 self.view?.addsubview(cell) gridarray[xpos][ypos] = cell
the 2d array being created on application load so:
gridarray = array(repeating: array(repeating: nil, count: gridsizey), count: gridsizex)
i have tried search solution remove uiviews superview answers other questions don't seem work me. have tried add cell.tag = 100
each uiview , remove uiviews tag value of 100 like:
for subview in (self.view?.subviews)! { if (subview.tag == 100) { subview.removefromsuperview() } }
however no luck. have tried use method:
self.view?.subviews.foreach({ $0.removeconstraints($0.constraints) $0.removefromsuperview() })
the main differences notice in code compared other peoples answers have "?" , "!" @ places in code. researched mean , understood of still not know how fix code because if it, , feel though issue. know attempts remove uiviews superview not work, , without "?" , "!"s code doesn't run @ all.
how create tag each cell using example:
//init value for tag. var n = 0 func preparecell() -> uiview { let xstart = int(size.width/2/2) - ((gridsizex * gridscale)/2) let ystart = int(size.height/2/2) - ((gridsizey * gridscale)/2) let cell : uiview! cell = uiview(frame: cgrect(x: xstart + (xpos * gridscale), y:ystart + (ypos * gridscale), width:gridscale, height:gridscale)) cell.layer.borderwidth = 1 cell.layer.bordercolor = uicolor(red:0.00, green:0.00, blue:0.00, alpha:0.02).cgcolor cell.backgroundcolor = uicolor(red:1.00, green:1.00, blue:1.00, alpha:0) cell.tag = n //each cell should have new value n += 1 return cell }
and remove required views.
func removeviews() { z in 0...n { if let viewwithtag = self.view.viewwithtag(z) { viewwithtag.removefromsuperview() } else { print("tag not found") } } }
example working in playground:
var n = 0 let mainview = uiview() func createview() -> uiview { let view = uiview() view.tag = n n += 1 return view } in 0...16 { mainview.addsubview(createview()) } func removeviews() { z in 0...n { if let viewwithtag = mainview.viewwithtag(z) { viewwithtag.removefromsuperview() print("removed") } else { print("tag not found") } } }
Comments
Post a Comment