How do I draw a shadow under a UIView?

uiview shadow top and bottom
uiview shadow one side swift
add shadow to uiview swift storyboard
uiview shadow not showing
uiview drop shadow and rounded corners swift 4
add shadow to uiview objective-c
uiview shadow top swift
uibezierpath shadow swift

I'm trying to draw a shadow under the bottom edge of a UIView in Cocoa Touch. I understand that I should use CGContextSetShadow() to draw the shadow, but the Quartz 2D programming guide is a little vague:

  1. Save the graphics state.
  2. Call the function CGContextSetShadow, passing the appropriate values.
  3. Perform all the drawing to which you want to apply shadows.
  4. Restore the graphics state

I've tried the following in a UIView subclass:

- (void)drawRect:(CGRect)rect {
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    CGContextSaveGState(currentContext);
    CGContextSetShadow(currentContext, CGSizeMake(-15, 20), 5);
    CGContextRestoreGState(currentContext);
    [super drawRect: rect];
}

..but this doesn't work for me and I'm a bit stuck about (a) where to go next and (b) if there's anything I need to do to my UIView to make this work?

In your current code, you save the GState of the current context, configure it to draw a shadow .. and the restore it to what it was before you configured it to draw a shadow. Then, finally, you invoke the superclass's implementation of drawRect: .

Any drawing that should be affected by the shadow setting needs to happen after

CGContextSetShadow(currentContext, CGSizeMake(-15, 20), 5);

but before

CGContextRestoreGState(currentContext);

So if you want the superclass's drawRect: to be 'wrapped' in a shadow, then how about if you rearrange your code like this?

- (void)drawRect:(CGRect)rect {
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    CGContextSaveGState(currentContext);
    CGContextSetShadow(currentContext, CGSizeMake(-15, 20), 5);
    [super drawRect: rect];
    CGContextRestoreGState(currentContext);
}

How to add a shadow to a UIView in Swift, from the path. The default value of this property is (0.0, -3.0). Let’s see this in two ways. Method 1 − Simply coding where ever required. Method 2 − Creating IBDesignable and IBInspectable and Using with Story board. Using the above extension for UIView we can make these properties accessible to all the Storyboards and play around with designs without

A by far easier approach is to set some layer attributes of the view on initialization:

self.layer.masksToBounds = NO;
self.layer.shadowOffset = CGSizeMake(-15, 20);
self.layer.shadowRadius = 5;
self.layer.shadowOpacity = 0.5;

You need to import QuartzCore.

#import <QuartzCore/QuartzCore.h>

What does shadowOffset do in iOS exactly?, on the cell's layer and the border on the contentView's layer to achieve both results. Enjoy! Set the layer attributes for the cell, not contentView . I understand that I should use CGContextSetShadow() to draw the shadow, but the Quartz 2D programming guide is a little vague: Save the graphics state. Call the function CGContextSetShadow, passing the appropriate values. Perform all the drawing to which you want to apply shadows. Restore the graphics state; I've tried the following in a UIView subclass:

self.layer.masksToBounds = NO;
self.layer.cornerRadius = 8; // if you like rounded corners
self.layer.shadowOffset = CGSizeMake(-15, 20);
self.layer.shadowRadius = 5;
self.layer.shadowOpacity = 0.5;

This will slow down the application. Adding the following line can improve performance as long as your view is visibly rectangular:

self.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.bounds].CGPath;

Adding rounded corner and drop shadow to UICollectionViewCell , iOS can dynamically generate shadows for any UIView , and these because iOS has to draw Duration: 2:29 Posted: Jun 1, 2019 "You can also create a drop shadow that will be based on the alpha component of whatever is drawn in your view. Often this will result in a shadow just around the edges of the view. Often this will result in a shadow just around the edges of the view.

Same solution, but just to remind you: You can define the shadow directly in the storyboard.

Ex:

How to add a shadow to a UIView, All subclasses of UIView , plus UIView itself, have access to a layer property a surface, so the shadow exists directly below the view rather than around it. To make this effect, we're going to create a custom shadow path. let yourView = UIView() yourView.layer.shadowColor = UIColor.black.cgColor yourView.layer.shadowOpacity = 1 yourView.layer.shadowOffset = .zero yourView.layer.shadowRadius = 10. Be warned: generating shadows dynamically is expensive, because iOS has to draw the shadow around the exact shape of your view's contents.

You can try this .... you can play with the values. The shadowRadius dictates the amount of blur. shadowOffset dictates where the shadow goes.

Swift 2.0

let radius: CGFloat = demoView.frame.width / 2.0 //change it to .height if you need spread for height
let shadowPath = UIBezierPath(rect: CGRect(x: 0, y: 0, width: 2.1 * radius, height: demoView.frame.height))
//Change 2.1 to amount of spread you need and for height replace the code for height

demoView.layer.cornerRadius = 2
demoView.layer.shadowColor = UIColor.blackColor().CGColor
demoView.layer.shadowOffset = CGSize(width: 0.5, height: 0.4)  //Here you control x and y
demoView.layer.shadowOpacity = 0.5
demoView.layer.shadowRadius = 5.0 //Here your control your blur
demoView.layer.masksToBounds =  false
demoView.layer.shadowPath = shadowPath.CGPath

Swift 3.0

let radius: CGFloat = demoView.frame.width / 2.0 //change it to .height if you need spread for height 
let shadowPath = UIBezierPath(rect: CGRect(x: 0, y: 0, width: 2.1 * radius, height: demoView.frame.height)) 
//Change 2.1 to amount of spread you need and for height replace the code for height

demoView.layer.cornerRadius = 2
demoView.layer.shadowColor = UIColor.black.cgColor
demoView.layer.shadowOffset = CGSize(width: 0.5, height: 0.4)  //Here you control x and y
demoView.layer.shadowOpacity = 0.5
demoView.layer.shadowRadius = 5.0 //Here your control your blur
demoView.layer.masksToBounds =  false
demoView.layer.shadowPath = shadowPath.cgPath

Example with spread

To create a basic shadow

    demoView.layer.cornerRadius = 2
    demoView.layer.shadowColor = UIColor.blackColor().CGColor
    demoView.layer.shadowOffset = CGSizeMake(0.5, 4.0); //Here your control your spread
    demoView.layer.shadowOpacity = 0.5 
    demoView.layer.shadowRadius = 5.0 //Here your control your blur

Basic Shadow example in Swift 2.0

Advanced UIView shadow effects using shadowPath – Hacking with , You probably had problems when you tried to add drop shadow to an imageView or a UIView. It is because of true for rounded corners. You can simple add a separate UIView… And give it the below property: innerView  The shadow is applied to the parent view’s layer, while the rounded corners are applied to the containerView. Then, just add all content to the containerView and be on your way.

Adding Shadow and Rounded Corner to UIView, To make our UI appealing, we have to play around with multiple attributes in iOS development. To draw shadows around a View or below a  Instead of drawing inner shadow by drawRect or add UIView to the View. You may directly Add CALayer to the border, for example: if I want inner shadow effect on UIView V's bottom.

How do I draw a shadow under a UIView in iPhone/iOS?, In order to display a shadow to a UIView in Swift, iOS allows you to specify its color, opacity, offset and radius. Published by florian on August 4, 2018 building the best iOS app templates that will help you make your own  UIView-InnerShadow. UIView+Shadow is a quick category I created to add an inner shadow to a UIView. This is useful for quickly adding an inner shadow to any view. It is configurable to allow shadows to grow from any or all of the view edges. Usage. ###All Directions Example. Here is an example of all borders with the default color

Draw a Shadow Under a UIView. · GitHub, Draw a Shadow Under a UIView. DrawShadowUnderUIView.m. self.layer.​masksToBounds = NO;. self.layer.cornerRadius = 8; // optional. self.layer.​shadowOffset  Back in 2014, I wrote a blog post on Expanding User-Defined Runtime Attributes in Xcode where I added a border, corner radius, and shadow to a UIView using Interface Builder’s user-defined runtime attributes. This solution had no type checking—you had to type the property you wanted to modify by hand and often had to look up what it was called.

Comments
  • But be aware that this only works on iOS 3.2+ so if your app should work on old version you have to use Christian's solution or an static image behind the view if this is an option.
  • This solution also requires adding #import <QuartzCore/QuartzCore.h>" to the .h file.
  • Setting masksToBounds to NO will negate the cornerRadius, no?
  • Okay, to solve that, the backgroundColor needs to be set on the layer and the view needs to be transparent.
  • @pixelfreak How do you do that? I tried self.layer.backgroundColor = [[UIColor whiteColor] CGColor]; but no luck. Which view needs to be transparent?
  • It's probably worth noting this optimisation is only useful if your view is visibly rectangular.
  • self.layer.shadowPath... instead of what? or just adding to it
  • Just add that extra line in addition to the others.
  • @NathanGaskin - drawing shadows is expensive operation, so for example if your application allows other interface orientation and you start to rotate the device, without specifying explicitly the path of the shadow it has to be rendered several times during that animation which will, depending on the shape, probably visibly slow down the animation
  • @BenjaminDobell That particular line only works for rectangles but you can also create non-rectangular paths. For example, if you have a rounded rectangle you can use bezierPathWithRoundedRect:cornerRadius:
  • Sadly, I think CGColor is out of reach from the storyboard :(
  • just define a category for UIView or CGLayer which is an UIColor wrapper for the CGColor property ;)