我将图层添加到我的视图中:
[self.layer insertSublayer:_gradient above:_another.layer];
然后即使我在超级层上调用 needsDisplay ,它似乎也不会被删除:
[_gradient removeFromSuperlayer];
如何删除它?
答案 0 :(得分:1)
你可以在从superlayer中删除后设置渐变nil。它对我有效。
[gradient removeFromSuperlayer];
gradient =nil;
答案 1 :(得分:0)
我使用了这些,然后检查以确保将其删除:
// assuming there is only 1 gradientLayer this should print 1
print(parentView.layer.sublayers?.count as Any)
parentView.layer.sublayers?.removeAll() // remove everything from the parentView
gradientLayer.removeFromSuperlayer() // just to be safe
// check to see if it's in the parentView or not
if let _ = (parentView.layer.sublayers?.compactMap { $0 as? CAGradientLayer })?.first {
print("gradientLayer has not been removed")
} else {
print("yay it's removed")
}
// this should now print nil
print(parentView.layer.sublayers?.count as Any)