可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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 answer.
I tried to do something like this:
for(CALayer *layer in rootLayer.sublayers)
{
[layer removeFromSublayer];
}
but it didn't work.
Also, i tried to clone rootLayer.sublayers into separate NSArray, but result was the same.
Any ideas?
Edit:
I thought it works now, but I was wrong. It works good with CALayers, but it doesn't work with CATextLayers. Any ideas?
回答1:
The simplest way to remove all sublayers from a layer is to set the sublayer property to nil:
rootLayer.sublayers = nil;
回答2:
The following should work:
for (CALayer *layer in [[rootLayer.sublayers copy] autorelease]) {
[layer removeFromSuperlayer];
}
回答3:
[rootLayer.sublayers makeObjectsPerformSelector:@selector(removeFromSuperlayer)];
回答4:
Swift (short version):
layer.sublayers?.forEach { $0.removeFromSuperlayer() }
回答5:
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.
回答6:
How about using reverse enumeration?
NSEnumerator *enumerator = [rootLayer.sublayers reverseObjectEnumerator];
for(CALayer *layer in enumerator) {
[layer removeFromSuperlayer];
}
Because the group in sublayers are changed during enumeration, if the order is normal.
I would like to know the above code's result.
回答7:
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)];
回答8:
I tried to delete just the first sublayer at index 0 and this worked:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([cell.contentView.layer.sublayers count] != 0) {
[[cell.contentView.layer.sublayers objectAtIndex:0] removeFromSuperlayer];
}
回答9:
Both setting self.view.layer.sublayers = nil
and [layer removeFromSuperlayer]
will result in crash if UIView
contains any subview. Best way to remove CALayer
from superLayer is by maintaining an array of layers.
For instance that array is arrayOfLayers,
Everytime you add a layer on UIView.sublayer
add that layer in this array...
For example
[arrayOfLayers addObject:layer];
[self.view.layer addSublayer:layer];
While removing just call this,
[arrayOfLayers makeObjectsPerformSelector:@selector(removeFromSuperlayer)];
回答10:
I had to do this in Xamarin/C#. I had a UITableViewCell
with some CAShapeLayers
for borders. All of the above options (including copying the Array and then removing layers caused a crash). The approach that worked for me:
When adding the CALayer
, I gave it a name:
var border = new CALayer();
border.BackgroundColor = color.CGColor;
border.Frame = new CGRect(x, y, width, height);
border.Name = "borderLayerName";
Layer.AddSublayer(border);
In PrepareForReuse
on the UITableViewCell
, I created a copy of the SubLayers
property and removed anything that matched the name I assigned earlier:
CALayer[] copy = new CALayer[Layer.Sublayers.Length];
Layer.Sublayers.CopyTo(copy, 0);
copy.FirstOrDefault(l => l.Name == "borderLayerName")?.RemoveFromSuperLayer();
No crashes.
Hope this helps!
回答11:
I have faced the same issue and found many solutions but none of them are perfect.
Finally from above answer of pnavk i got the idea. At there the code is for Xamarin/C# users.
The iOS version could be as below:
Swift 2.3
if rootLayer.sublayers?.count > 0 {
rootLayer.sublayers?.forEach {
if $0.name == "bottomBorderLayer" {
$0.removeFromSuperlayer()
}
}
}
let border = CALayer()
let height = CGFloat(1.0)
border.borderColor = UIColor.blackColor().CGColor
border.borderWidth = height
border.frame = CGRectMake(0, self.frame.size.height - height, self.frame.size.width, self.frame.size.height)
border.name = "bottomBorderLayer"
rootLayer.addSublayer(border)
rootLayer.masksToBounds = true
Objective-C
if (rootLayer.sublayers.count > 0) {
for (CALayer *layer in rootLayer.sublayers) {
if ([layer.name isEqualToString:@"bottomBorderLayer"]) {
[layer removeFromSuperlayer];
}
}
}
CALayer *border = [[CALayer alloc] init];
CGFloat height = 1.0;
border.borderColor = [UIColor blackColor].CGColor;
border.borderWidth = height;
border.frame = CGRectMake(0, self.view.frame.size.height - height, self.view.frame.size.width, self.view.frame.size.height);
border.name = @"bottomBorderLayer";
[rootLayer addSublayer:border];
rootLayer.masksToBounds = TRUE;
The above code will work for bottom border only. You can change the border side as per your requirement.
Here before adding any layer to the controller, I have just run a for loop to check whether any border is already applied or not?
To identify previously added border I use name property of CALayer. And compare that layer before remove from sublayers.
I have tried the same code before using name property, but It creates random crash. But after using name property and comparing name before remove will solve the crash issue.
I hope this will help someone.
回答12:
For swift3+,You can use this.
yourView.layer.sublayers?.removeAll(keepingCapacity: true)
See here: Apple API Reference
Or you can:
yourView.layer.sublayers?.forEach{ $0.removeFromSuperlayer()}
回答13:
What about doing:
rootLayer.sublayers = @[];
回答14:
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.