Removing all CALayer's sublayers

前端 未结 15 981
说谎
说谎 2020-12-07 20:25

I have trouble with deleting all of layer\'s sublayers. I currently do this manually, but that brings unnecessary clutter. I found many topics about this in google, but no a

相关标签:
15条回答
  • 2020-12-07 20:46

    Swift (short version):

    layer.sublayers?.forEach { $0.removeFromSuperlayer() }

    0 讨论(0)
  • 2020-12-07 20:50

    For sure you can do:
    self.layer.sublayers=nil;
    as suggested by Pascal Bourque. But it's better to call the setter for sublayers property:
    [self.layer setSublayers:nil];
    To avoid any clean up issues if there might be.

    0 讨论(0)
  • 2020-12-07 20:55

    Indiscriminately removing all sublayers causes nasty crashes in iOS 7, which can occur much later in the program's execution. I have tested this thoroughly using both rootLayer.sublayers = nil and [rootLayer.sublayers makeObjectsPerformSelector:@selector(removeFromSuperlayer)]. There must be a system-created layer that's getting messed up.

    You have to keep your own array of layers and remove them yourself:

    [myArrayOfLayersIAddedMyself makeObjectsPerformSelector:@selector(removeFromSuperlayer)];
    
    0 讨论(0)
提交回复
热议问题