Corner Radius for mask layer drawn on UIView - ios

I am drawing mask path on UIView to make it look like a rectangle at a particular point of view. I want to set corner radius for the mask path so that it can appear as bulged at the side like popover of iOS. Any idea how to implement.
Thanks

Use this Code,
Objective C code:
// Cornor radius
[view.layer setCornerRadius:30.0f];
view.layer.masksToBounds = YES;
// border
[view.layer setBorderColor:[UIColor lightGrayColor].CGColor];
[view.layer setBorderWidth:1.5f];
// drop shadow
[view.layer setShadowColor:[UIColor blackColor].CGColor];
[view.layer setShadowOpacity:0.8];
[view.layer setShadowRadius:3.0];
[view.layer setShadowOffset:CGSizeMake(2.0, 2.0)];
Swift Code:
// Cornor radius
view.layer.cornerRadius = 10.0
view.layer.masksToBounds = true
// border color and width
view.layer.borderColor = UIColor.blueColor().CGColor
view.layer.borderWidth = 2.0
// drop shadow
view.layer.shadowColor = UIColor.blackColor().CGColor
view.layer.shadowOpacity = 1.0
view.layer.shadowRadius = 3.0
view.layer.shadowOffset = CGSizeMake(2.0, 2.0)
hope its helpful

Related

How to draw shadow around the masked UIView?

I want to make a UIView which will have shape like in Figure 1. and also should have shadow around the shape of my view .
I am using below code to draw CAShaperLayer with UIBezierPath
UIBezierPath *bezier = [UIBezierPath bezierPath];
[bezier moveToPoint:CGPointMake(0,7)];
[bezier addLineToPoint:CGPointMake(self.menuView.frame.size.width-15, 7)];
[bezier addLineToPoint:CGPointMake(self.menuView.frame.size.width-10,0)];
[bezier addLineToPoint:CGPointMake(self.menuView.frame.size.width-5, 7)];
[bezier addLineToPoint:CGPointMake(self.menuView.frame.size.width, 7)];
[bezier addLineToPoint:CGPointMake(self.menuView.frame.size.width, self.menuView.frame.size.height)];
[bezier addLineToPoint:CGPointMake(0, self.menuView.frame.size.height)];
[bezier closePath];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.path = [bezier CGPath];
self.menuView.layer.mask = maskLayer;
and for adding shadow
self.menuView.layer.shadowColor = [[UIColor colorWithRed:8.0f/255.0f green:37.0f/255.0f blue:82.0f/225.0f alpha:1] CGColor];
self.menuView.layer.shadowOffset = CGSizeZero;
self.menuView.layer.shadowOpacity = 0.3f;
self.menuView.layer.shadowRadius = 5.0f;
self.menuView.layer.masksToBounds=NO;
My Problem :
After masking CAShaperLayer on menuView ,the shadow has disappeared . Figure 2
Instead of Masking View , i also tried to add Sublayer on it
[self.menuView.layer addSublayer:maskLayer]
but this will hide my all SubViews of MenuView Figure 3
(I have changed the CASahperLayer Color to Black just to show )
🔹 My Question is :
What is the best way to get the perfect shape with shadow as shown in Figure 1 ???.
(Thanks in advance)
extension UIView{
func viewShadowBorder() {
self.layer.shadowColor = UIColor.gray.cgColor
self.layer.shadowOpacity = 1
self.layer.shadowOffset = CGSize.zero
self.layer.shadowRadius = 2
self.layer.cornerRadius = 2
}
}

How to do colour shading in Objective-C

I need to do a colour shading in objective c. As shown in this image shade is on the button of HardService and SoftService.
Is there is any way to do this ? Thanks in Advance!
CAGradientLayer would server your purpose - Reference:
http://blog.apoorvmote.com/gradient-background-uiview-ios-swift/
You can use either a gradient image or CAGradientLayer for this purpose
CAGradientLayer added to an existing button:
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = button.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor whiteColor] CGColor], (id)[[UIColor blackColor] CGColor], nil];
[button.layer insertSublayer:gradient atIndex:0];
You can use an image for showing shadow. Use blur image or change alpha of the imageview.
Or if you want to do it programmatically, try it:
Obj c:
//create elliptical shadow for button through UIBezierPath
CGRect ovalRect = button.bounds;
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:ovalRect];
//applying shadow to path
button.layer.shadowColor = [UIColor yourcolor].CGColor;
button.layer.shadowOffset = CGSizeMake(0.0, 0.0);
button.layer.shadowOpacity = 1.0;
button.layer.shadowRadius = 3.0;
button.layer.shadowPath = path.CGPath;
Swift :
//create elliptical shdow forimage through UIBezierPath
var ovalRect = button.bounds
var path = UIBezierPath(ovalInRect: ovalRect)
//applying shadow to path
button.layer.shadowColor = UIColor(white: 0.0, alpha: 0.5).CGColor
button.layer.shadowOffset = CGSizeMake(0.0, 0.0)
button.layer.shadowOpacity = 1.0
button.layer.shadowRadius = 3.0
button.layer.shadowPath = path.CGPath
Taken from http://www.innofied.com/implementing-shadow-ios/ and also have a look to know more: UIView with rounded corners and drop shadow?
My another answer related to this: Drop shadow in ios

iOS: CAShapeLayer, Change color of outer layer

I am currently using CAShapeLayer for one of my requirement. I was successful in implementing the fillcolor, strokecolor, etc... but I wanted to change the color of outer part of that CAShapeLayer. I tried doing it with backgroundcolor, & followed many answers from SO, but was unable to do it. Could anyone guide me with the solution. Screenshot attached
Edit 1: Code for creating the layer
// create layer mask for map
CAShapeLayer *maskLayer = [CAShapeLayer layer];
mapView.layer.mask = maskLayer;
// maskLayer.backgroundColor = [[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.7] CGColor];
self.maskLayer = maskLayer;
// create shape layer for circle we'll draw on top of map (the boundary of the circle)
CAShapeLayer *circleLayer = [CAShapeLayer layer];
circleLayer.lineWidth = 3.0;
circleLayer.fillColor = [[UIColor clearColor] CGColor];
circleLayer.strokeColor = [[UIColor blackColor] CGColor];
// circleLayer.borderColor = [[UIColor blackColor] CGColor];
// circleLayer.backgroundColor = [[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.7] CGColor];
[mapView.layer addSublayer:circleLayer];
self.circleLayer = circleLayer;
Edit 2: Xcode's Viewer's Debugger
In the above image I can see that I have set the background color to self.view. But I want it to be over my map view & the the color should be semi transparent so that map data outside circle should also be visible.
Have you tried using XCode views's debugger to know where this white background belongs in your view hierarchy ?
You're using your view's maskLayer as a circle. So my guess is that this white background must be OUTSIDE your view - everything out your mask gets clipped - so you should probably try to change your viewController's view backgroundColor (or any other view that is just above your custom rounded view)
[self.view.layer setBackgroundColor:[UIColor grayColor].CGColor];
UIBezierPath *circle = [UIBezierPath bezierPathWithArcCenter:self.view.center
radius:45.0
startAngle:0
endAngle:2.0*M_PI
clockwise:YES];
CAShapeLayer *circleLayer = [CAShapeLayer layer];
circleLayer.bounds = CGRectMake(0, 0, 2.0*(45.0), 2.0*(45.0));
circleLayer.path = circle.CGPath;
circleLayer.strokeColor = [UIColor orangeColor].CGColor;
circleLayer.fillColor = [UIColor whiteColor].CGColor;
circleLayer.lineWidth = 3.0; // your line width
[self.view.layer addSublayer:circleLayer];
See if it helps you.

Why don't added sublayers show up in screenshot?

I'm trying to track down a bug in some iOS code for an iPad app. In one of our views, we've added sublayers to have a shadow and make sure the bottom of the view has rounded edges. Here's the code where we add the sublayers:
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
byRoundingCorners:(UIRectCornerBottomLeft|UIRectCornerBottomRight)
cornerRadii:CGSizeMake(12.0f, 12.0f)];
// Create the shadow layer
shadowLayer = [CAShapeLayer layer];
[shadowLayer setFrame:self.bounds];
[shadowLayer setMasksToBounds:NO];
[shadowLayer setShadowPath:maskPath.CGPath];
shadowLayer.shadowColor = [UIColor blackColor].CGColor;
shadowLayer.shadowOffset = CGSizeMake(0.0f, 0.0f);
shadowLayer.shadowOpacity = 0.5f;
shadowLayer.shadowRadius = 6.0f;
roundedLayer = [CALayer layer];
[roundedLayer setFrame:self.bounds];
[roundedLayer setBackgroundColor:[UIColor colorFromHex:#"#e4ecef"].CGColor];
[self.layer insertSublayer:shadowLayer atIndex:0];
// Add inner view (since we're rounding corners, parent view can't mask to bounds b/c of shadow - need extra view)
maskLayer = [CAShapeLayer layer];
maskLayer.frame = self.bounds;
maskLayer.path = maskPath.CGPath;
innerView = [[UIView alloc] initWithFrame:self.bounds];
innerView.backgroundColor = [UIColor whiteColor];
innerView.layer.mask = maskLayer;
[self addSubview:innerView];
It shows up fine on the screen of the iPad, but I want to take a screenshot programmatically. I've added a category to UIView with this method:
- (UIImage*)screenshot {
UIGraphicsBeginImageContext(self.frame.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
return viewImage;
}
When I look at the screenshot that is taken, it no longer has the rounded corners or the shadow behind my view. Why aren't they showing up?
Found an explanation here: CALayer renderInContext
Additionally, layers that use 3D transforms are not rendered, nor are
layers that specify backgroundFilters, filters, compositingFilter, or
a mask values.
It looks like some sublayers can't be handled by renderInContext, which is why they aren't showing up in my screenshots.
Gradient sublayers
The snapshot is not taking any gradient layers with alpha channel in it.
Remove the alpha from colors, works for me

creating border for uitableview

I am using a uitableview and scrollview in a uiview. How to set a border for table or scrollview?
#import "QuartzCore/QuartzCore.h"
then in viewDidLoad use,
tableView.layer.borderWidth = 2.0;
Note
You can also set the border color:
tableView.layer.borderColor = [UIColor redColor].CGColor;
It should work for you. :)
This works quite well for me:
CALayer *layer = _tableView.layer;
[layer setMasksToBounds:YES];
[layer setCornerRadius: 4.0];
[layer setBorderWidth:1.0];
[layer setBorderColor:[[UIColor colorWithWhite: 0.8 alpha: 1.0] CGColor]];
The rounded borders look a little more iOS.
if you want to give the border to tableview with color, use below code
for swift 3
yourTableViewName.layer.borderColor = UIColor.gray.cgColor
yourTableViewName.layer.borderWidth = 1.0

Resources