remove all subLayers from a view

前端 未结 9 920
春和景丽
春和景丽 2021-02-01 01:16

In an animation I added a lot of sublayers to a view, with:

[self.view.layer addSublayer:layer1];
[self.view.layer addSublayer:         


        
相关标签:
9条回答
  • 2021-02-01 01:50

    Swift 3.0 & Swift 4.0

    Set the sublayers property to nil to remove all sublayers from a view.

    view.layer.sublayers = nil
    

    also you can add

    .removeAll()
    
    0 讨论(0)
  • 2021-02-01 01:53

    Simple one liner in SWIFT 4.

    self.view.layer.sublayers.removeAll()
    
    0 讨论(0)
  • 2021-02-01 01:55

    If you want to delete all sublayers and add a new one, you can easily do:

    rootLayer.sublayers = [layer1] // adding one layer
    
    rootLayer.sublayers = [layer1, layer2, ...] // adding two or more layers
    

    This could be helpful, if you work with tableview or collectionview cells. You don't need to call prepareForReuse for removing the sublayers.

    0 讨论(0)
提交回复
热议问题