I ran into the same problem and found many solutions, but none of them are perfect. Finally, from the above pnavk answer , I got an idea. There is code for Xamarin / C # users.
The iOS version may be as follows:
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 code above will only work for the lower bound. You can change the border side according to your requirement.
Here, before adding any level to the controller, I just ran a for loop to check if any border is applied or not?
To identify a previously added border, I use the CALayer name property. And compare this layer before removing it from the sublayers.
I used the same code before using the name property, but it creates a random crash. But after using the name property and comparing the name before the deletion, the failure problem will be fixed.
I hope this helps someone.
Er. Vihar Sep 12 '17 at 7:26 2017-09-12 07:26
source share