Adding a shadow at the edge of the UIView(Picture inside) - ios

So, I am looking for the most practical way to adding a shadow at the edge of UIView like shown in the picture below. I tried some ways that I read but really couldn't make them work as I wanted. It would be nice if someone had an idea how apple does this in their apps.
Picture link(Didn't want to post it directly here as it was pretty big): Example image

The following properties deal with shadows on UIViews:
view.layer.shadowOpacity = 0.5;
view.layer.shadowRadius = 25;
view.layer.shadowColor = [UIColor blackColor].CGColor;
view.layer.shadowOffset = CGSizeMake(-3, 0);
That code will make a black shadow at half opacity, with a shadow radius of 25 pixels, and shifted 3 pixels to the left. You'll need to add the following import to the top of the file:
#import <QuartzCore/QuartzCore.h>
You may also need to add the QuartzCore framework to your project if it isn't already.

- (void) configureSlideLayer:(CALayer *)layer
{
layer.shadowColor = [UIColor blackColor].CGColor;
layer.shadowOpacity = 1;
layer.shadowOffset = CGSizeMake(-3, 0);
layer.shadowRadius = 5;
layer.masksToBounds = NO;
layer.shadowPath =[UIBezierPath bezierPathWithRect:layer.bounds].CGPath;
}
...
[self configureSlideLayer:self.view.layer];
source: https://github.com/arturdev/AMSlideMenu

Related

iOS Button with round corners and a shadow

I have searched and tried the various solutions but without success. What I would like is a button background with rounded corners and a shadow. I can make one or the other happen but not both at the same time. Any help would be very welcome.
viewDepositButton_.layer.cornerRadius = 5.0;
CAGradientLayer *viewLayer = [CAGradientLayer layer];
[viewLayer setColors:aloColors];
[viewLayer setFrame:viewDepositButton_.bounds];
[viewDepositButton_.layer insertSublayer:viewLayer atIndex:0];
viewDepositButton_.clipsToBounds = YES;
viewDepositButton_.layer.shadowColor = [UIColor colorWithRed:0.46 green:0.46 blue:0.46 alpha:1.0].CGColor;
viewDepositButton_.layer.shadowOpacity = 0.8;
viewDepositButton_.layer.shadowRadius = 8;
viewDepositButton_.layer.shadowOffset = CGSizeMake(8.0f, 8.0f);
viewDepositButton_.clipsToBounds = YES; or viewDepositButton_.layer.masksToBounds = YES; will clip everything outside your layer - including the shadow.
However you have few options:
Put your button under a parent view, which will have the same corner radius and the shadow you want, but will have clipsToBounds = NO. This way you'll have your button as a subview of a view which has the shadow.
Use an image for your button, which has the rounded corners and set your buttons clipsToBounds to NO
Hope this helps
I was able to get your code to work by the following and it looks fine. Make sure you call your methods in proper sequence.
viewDepositButton_.layer.cornerRadius = 5.0;
viewDepositButton_.layer.borderWidth = 1.0;
viewDepositButton_.layer.shadowColor = [UIColor colorWithRed:0.46 green:0.46 blue:0.46 alpha:1.0].CGColor;
viewDepositButton_.layer.shadowRadius = 8;
viewDepositButton_.layer.shadowOpacity = 0.8;
viewDepositButton_.layer.shadowOffset = CGSizeMake(8.0f, 8.0f);
CAGradientLayer *viewLayer = [CAGradientLayer layer];
[viewLayer setColors:aloColors];
[viewLayer setFrame:viewDepositButton_.bounds];
[viewDepositButton_.layer insertSublayer:viewLayer atIndex:0];
[viewDepositButton_ viewLayer];}
Here is my code for setting up a rounded corner button with shadow.
button.layer.cornerRadius = 15;
button.layer.shadowRadius = 2.0f;
button.layer.shadowColor = [UIColor lightGrayColor].CGColor;
button.layer.shadowOffset = CGSizeMake(-1.0f, 3.0f);
button.layer.shadowOpacity = 0.8f;
button.layer.masksToBounds = NO;
Overall, the code is pretty straightforward. Please comment if you have any question, concerns or bugs.

UILabel Background Color Leaks to Border

I'm creating a UILabel to which I set the background color and corner radius with the following code:
self.scoreLabel.backgroundColor = [UIColor DISRed];// custom red`
self.scoreLabel.layer.masksToBounds = YES;
self.scoreLabel.layer.cornerRadius = self.scoreLabel.frame.size.width/2;
self.scoreLabel.layer.borderWidth = 8.0;
self.scoreLabel.layer.borderColor = [[UIColor DISNavy] CGColor];
However the background's color seems to be leaking to the edge of the border (see image). Any ideas why? Any idea on how to fix it?
I was also facing the same problem. It was a silly mistake. I always forget to tick clipToBounds in case of cornerRadius.
So, just ticking the Clip to Bounds for UILabel in Storyboard fixed my problem.
And yes, we need to keep the below code too:
label.layer.masksToBounds = true
I ran into the same problem with the UIButton's background color leaking around the edge of its border.
Instead of setting the UIButton background color on the UIButton, set it on the UIButton's layer.
Replace:
self.scoreLabel.backgroundColor = [UIColor DISRed];// custom red`
With this:
self.scoreLabel.layer.backgroundColor = [[UIColor DISRed] CGColor];// custom red`
I created my own UILabel and background colour does not seem to be leaking.
Write this in .h file of your project.
UILabel *label;
Write this in .m file of your project.
label=[[UILabel alloc]initWithFrame:CGRectMake(100, 300, 100, 100)];//Set frame of label in your viewcontroller.
[label setBackgroundColor:[UIColor redColor]];//Set background color of label.
[label setText:#"Label"];//Set text in label.
[label setTextColor:[UIColor blackColor]];//Set text color in label.
[label setTextAlignment:NSTextAlignmentCenter];//Set text alignment in label.
[label.layer setCornerRadius:50.0];//Set corner radius of label to change the shape.
[label.layer setBorderWidth:8.0f];//Set border width of label.
[label setClipsToBounds:YES];//Set its to YES for Corner radius to work.
[label.layer setBorderColor:[UIColor greenColor].CGColor];//Set Border color.
[self.view addSubview:label];//Add it to the view of your choice.
It is probably anti aliasing issue. you can better fix it by adding a bezier path around the corners.
CAShapeLayer *subLayer = [[CAShapeLayer alloc] init];
[subLayer setFillColor:[UIColor clearColor].CGColor];
[subLayer setStrokeColor:[UIColor whiteColor].CGColor];
[subLayer setLineWidth:1.0];
[subLayer setPath:[UIBezierPath bezierPathWithRoundedRect:imageView.bounds cornerRadius:imageView.layer.cornerRadius].CGPath];
[imageView.layer addSublayer:subLayer];
For those who are still facing the issue of border color leaking out:
Go through the below code, please note you will need to set frames & border width as per your requirement, I'm setting the position as view's center
let badgeSize: CGFloat = 10
let redBadge = UIView(frame: CGRect(x: view.center.x, y:view.center.y, width: badgeSize, height: badgeSize))
redBadge.layer.borderColor = UIColor.white.cgColor
redBadge.layer.borderWidth = 2
redBadge.backgroundColor = .red
redBadge.layer.cornerRadius = badgeSize * 0.5
redBadge.clipsToBounds = true
redBadge.layer.masksToBounds = true
redBadge.maskLayerOnView(radius: badgeSize * 0.5)
view.addSubview(redBadge)
Secondly, we need to write an extension on UIView
extension UIView{
func maskLayerOnView(radius: CGFloat){
let maskLayer = CAShapeLayer()
maskLayer.path = UIBezierPath(roundedRect: self.bounds,
byRoundingCorners: [.allCorners],
cornerRadii: CGSize(width: radius,
height: radius)).cgPath
self.layer.mask = maskLayer
}
}
This code snippet removes the border color separating out, one can replicate this behaviour on any kind of views.
For detailed explanation please see this article.
Well, a lot of answers...
I found the problem persists, as long as the UIViews background color is used and not the background color of the UIViews 'layer'. In addition, of course, masking needs to be enabled.
For a UICollectionViewCell subclass the code is:
- (void)prepare
{
self.contentView.layer.backgroundColor = UIColor.redColor.CGColor;
self.contentView.layer.cornerRadius = 25.0;
self.contentView.layer.borderColor = UIColor.blackColor.CGColor;
self.contentView.layer.borderWidth = 1.0;
//Enable to optimize image views: self.contentView.layer.shouldRasterize = YES;
//Enable to optimize image views: self.contentView.layer.rasterizationScale = UIScreen.mainScreen.scale;
self.contentView.layer.masksToBounds = YES;
self.contentView.clipsToBounds = YES;
}
To make the setting of the background color more comfortable and less error prone, some could add this:
/*
setBackgroundColor:
*/
- (void)setBackgroundColor:(UIColor *)p_BackgroundColor
{
super.backgroundColor = nil;
self.contentView.layer.backgroundColor = p_BackgroundColor.CGColor;
self.contentView.layer.masksToBounds = YES;
self.contentView.clipsToBounds = YES;
}

Drop a shadow to right and bottom of uiview

I have to drop a shadow to the right and bottom of uiview.Im doing this in interface builder.But I see the shadow dropped to top of it.Tried differnt sizes.but couldn't get it.
layer.masksToBound=No
layer.shadowOpacity=0.15
layer.shadowRadius=2
layer.shadowOffSet={10,-10} //Values being set in Interfacebuilder.
Still this drops shadow at top.What should I do to get at bottom of view.
Try the following code, it might help you
myView.layer.shadowColor = [UIColor purpleColor].CGColor;
myView.layer.shadowOffset = CGSizeMake(5, 5);
myView.layer.shadowOpacity = 1;
myView.layer.shadowRadius = 1.0;
myView.layer.maskToBounds = NO;
I tested this code and it's working and output is:
Hi I have used below code ,it will provide you with shadow you want.
UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:_viewShadow.bounds];
_viewShadow.layer.masksToBounds = NO;
_viewShadow.layer.shadowColor = [UIColor blackColor].CGColor;
_viewShadow.layer.shadowOffset = CGSizeMake(10.0f, 5.0f); /*Change value of X n Y as per your need of shadow to appear to like right bottom or left bottom or so on*/
_viewShadow.layer.shadowOpacity = 0.5f;
_viewShadow.layer.shadowPath = shadowPath.CGPath;
Also masksToBounds is imp as it disables the clipping of sublayers that extend further than the view's bounds. If you put it YES then you won't see shadow as it clips sublayer where else in NO it allow to extent layer.
In Swift 3, CGSizeMake no longer exists. It has been changed to CGSize(width: 20, height: 10). So the shadowOffset can be set like this in Swift 3:
myView.layer.shadowOffset = CGSize(width: 20, height: 10)
I found out that these values give a nice result :
myView.layer.shadowColor = UIColor.black.cgColor
myView.layer.shadowOpacity = 0.25
myView.layer.shadowRadius = 3
myView.layer.shadowOffset = CGSize(width: 1, height: 1) // shadow on the bottom right
I think your shadow offset is incorrect. It should be { 10 , 10} like:
[layer setShadowOffset:CGSizeMake( 10 , 10 ) ];

iOS6 Add Shadow to A Container UIView With A Corner Radius

How can an encompassing UIView have both a shadow and a corner radius at the same time?
I have tried other solutions suggested on SO multiple times, but unfortunately they do not seem to work for iOS6 (or at least not for me)
So I thought I might post this, so that an iOS6 solution can be found.
I have a container UIView which contains two subviews
- a custom UIImageView
- a custom UIView
I would like the entire UIView to have a corner radius of 2.5, but I would also like the UIView to have a shadow. However, so far, I get only 1 of these 2 desires, never both at the same time.
Here is my code, I have different versions of it with my different attempts with SO solutions, but this is just one of my versions.
self.layer.shouldRasterize = YES;
self.layer.rasterizationScale = [UIScreen mainScreen].scale;
self.layer.cornerRadius = 2.5;
self.layer.masksToBounds = YES;
self.layer.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.1].CGColor; //0.1
self.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.bounds].CGPath;
self.layer.shadowOpacity = 1.0;
self.layer.shadowRadius = 3.0;
^here self is the containing custom UIView with two subviews described above
Does anyone know of an iOS6 solution to this problem?
UPDATE
So, I do not need a border colour, so I was not adding that when I saw the solutions, but this time I added that, using the solution in the comment below, and it seems that the UIView is getting rounded, but I really want the combined UIImageView and UIView to get rounded.
So basically, the UIImageView is on top, and the UIView is on the bottom.
So how do I get only the top of the UIImageView to be rounded, and only the bottom of the UIView to be rounded.
Thanks.
Note: shadows work as one whole object, but the corner radius is not working as one whole object?
I figured it out.
self.layer.shouldRasterize = YES;
self.layer.rasterizationScale = [UIScreen mainScreen].scale;
self.layer.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.8].CGColor;
self.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:self.layer.bounds cornerRadius:self.layer.cornerRadius].CGPath;
self.layer.shadowOpacity = 1.0;
self.layer.shadowRadius = 3.0;
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
[self addSubview:container];
[container addSubview:self.someCustomUIView];
[container addSubview:self.someCustomImageView];
container.layer.cornerRadius = 2.5;
container.layer.masksToBounds = YES;
So basically:
I set the shadow of the main UIView.
I created a container subview which contains two other subviews
I set the corner radius of the container subview
Voila! It works!
I hope this works for other people who have multiple subviews in one UIView
I'd like to thank everyone for their help. :)
I think you should change this line of code:
self.layer.masksToBounds = YES;
to this one
self.layer.masksToBounds = NO;
If you set masksToBounds to YES, then you won't see anything that extends beyond the view's bounds, and this is the case with a shadow.
This code is from my current project (iOS 6) and it works fine. I can see both rounded corners and a shadow.
self.layer.masksToBounds = NO;
self.layer.cornerRadius = 5.0;
self.layer.shadowColor = [UIColor blackColor].CGColor;
self.layer.shadowOffset = CGSizeMake(0, -1);
self.layer.shadowOpacity = 0.6;
UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect: self.layer.bounds];
self.layer.shadowPath = shadowPath.CGPath;

IOS - Quartzcore custom UIView with shadow

I'm implementing a custom UIView to display inside a UIScrollview. The thing is that I need the view to drop a shadow so I did:
#import <QuartzCore/QuartzCore.h>
#implementation CustomView
-(void)setupView{
self.layer.shadowColor = [UIColor blackColor].CGColor;
self.layer.shadowOpacity = 0.5;
self.layer.shadowRadius = 1;
self.layer.shadowOffset = CGSizeMake(.6f, .6f);
self.layer.cornerRadius = 2;
[...]
}
-(id)initWithFrame:(CGRect)frame{
if((self = [super initWithFrame:frame])){
[self setupView];
}
return self;
}
[...]
The point is that when I build and run this the scrollview is so slow and I just need to remove those lines where I was hacking the "self.layer" and The scrollview goes fast and smooth again.
whats the proper way to add shadows to my custom View?
This has to do with the all the redrawing that the UIView has to do when moving.
If you rasterize the layer it will become way smoother, this should do the trick:
self.layer.rasterizationScale = [UIScreen mainScreen].scale;
self.layer.shouldRasterize = YES;
You could try to add a shadow path:
self.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.bounds];
This might help a bit more.

Resources