Subview appears underneath superviews layer.border?

According to the Apple specification: It is composited above the receiver’s contents and sublayers. So, the border will always be above of all your subviews, even if you bring your subview to the front and so on. So I make a background view to fake the border. E.g.: UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, … Read more

How is the relation between UIView’s clipsToBounds and CALayer’s masksToBounds?

They are different names because UIView and CALayer are different and have different terminology associated with them, but they are functionally equivalent. If you disassemble clipsToBounds you will see it just calls masksToBounds (disassembly from the simulator framework, so x86): -(BOOL)[UIView(Rendering) clipsToBounds] +0 3091938a 55 pushl %ebp +1 3091938b 89e5 movl %esp,%ebp +3 3091938d e800000000 … Read more

add UIImage in CALayer

This is a general answer for the sake of future viewers. It is based on the question title rather than the details of the original question. How to add a UIImage to a CALayer You can add an image to a view’s layer simply by using its contents property: myView.layer.contents = UIImage(named: “star”)?.cgImage Note that … Read more

CALayers didn’t get resized on its UIView’s bounds change. Why?

I used the same approach that Solin used, but there’s a typo in that code. The method should be: – (void)layoutSubviews { [super layoutSubviews]; // resize your layers based on the view’s new bounds mylayer.frame = self.bounds; } For my purposes, I always wanted the sublayer to be the full size of the parent view. … Read more

CALayer with transparent hole in it

I was able to solve this with Jon Steinmetz suggestion. If any one cares, here’s the final solution: int radius = myRect.size.width; UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, self.mapView.bounds.size.width, self.mapView.bounds.size.height) cornerRadius:0]; UIBezierPath *circlePath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 2.0*radius, 2.0*radius) cornerRadius:radius]; [path appendPath:circlePath]; [path setUsesEvenOddFillRule:YES]; CAShapeLayer *fillLayer = [CAShapeLayer layer]; fillLayer.path = path.CGPath; fillLayer.fillRule = kCAFillRuleEvenOdd; … Read more

Swift – Cut hole in shadow layer

Fortunately it’s now very easy today (2020) These days it’s very easy to do this: Here’s the whole thing import UIKit class GlowBox: UIView { override func layoutSubviews() { super.layoutSubviews() backgroundColor = .clear layer.shadowOpacity = 1 layer.shadowColor = UIColor.red.cgColor layer.shadowOffset = CGSize(width: 0, height: 0) layer.shadowRadius = 3 let p = UIBezierPath( roundedRect: bounds.insetBy(dx: 0, … Read more

UIView with a Dashed line

Check UIBezierPath setLineDash:count:phase: method: – (void)setLineDash:(const CGFloat *)pattern count:(NSInteger)count phase:(CGFloat)phase` method. This allows you to draw dashed lines. First add a CAShapeLayer. Add it as sublayer to your UIView. It has a path property. Now make an object of UIBezierPath. Draw the line using setLineDash. For example: UIBezierPath *path = [UIBezierPath bezierPath]; //draw a line … Read more

CALayer: add a border only at one side

I made a right border using this: leftScrollView.clipsToBounds = YES; CALayer *rightBorder = [CALayer layer]; rightBorder.borderColor = [UIColor darkGrayColor].CGColor; rightBorder.borderWidth = 1; rightBorder.frame = CGRectMake(-1, -1, CGRectGetWidth(leftScrollView.frame), CGRectGetHeight(leftScrollView.frame)+2); [leftScrollView.layer addSublayer:rightBorder];