Removing all CALayer's sublayers

前端 未结 15 1067
说谎
说谎 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:34

    Calling rootLayer.sublayers = nil; can cause a crash (e.g. if, under iOS 8, you call removeFromSuperview twice on the view owning rootLayer).

    The right way should be:

    [[rootLayer.sublayers copy] makeObjectsPerformSelector:@selector(removeFromSuperlayer)]

    The call to copy is needed so that the array on which removeFromSuperlayer is iteratively called is not modified, otherwise an exception is raised.

提交回复
热议问题