In figma, there's an effect you can apply to a view called a layer blur.
I cannot figure out how to replicate this sort of effect as its own view in Swift.
For example, say I want to add a view that goes under a button with a layer effect as so:
I have tried exporting the blur view from figma and using it as an image in code, but it just ends up like this:
These are the settings for the layer blur view I want to implement:
Any and all help would be greatly appreciated.
You can add drop shadow to a button this way.
Add an extension of UIView Like this.
extension UIView{
func dropShadow() {
layer.masksToBounds = false
layer.shadowColor = UIColor.black.cgColor
layer.shadowOpacity = 0.16
layer.shadowOffset = CGSize(width: 0, height: 3)
layer.shadowRadius = 3
}
}
Add drop shadow to your button by adding this line.
yourButton.dropShadow()
enter image description hereYou can add a shadow to your Button(View) for that purpose
view.layer.shadowColor = UIColor.black.cgColor
view.layer.shadowRadius = 20
view.layer.shadowOpacity = 1
view.layer.shadowOffset = .zero
shadowOpacity: sets the transparency, where 0 will be without shadow, and 1 is the strongest.
shadowRadius: sets how wide the shadow is.
shadowOffset: sets the distance between the view and shadow.
Related
Following is my code to create CardView and add shadow effect
override func awakeFromNib() {
super.awakeFromNib()
self.layoutIfNeeded()
cellBgview.layer.cornerRadius = 15.0
cellBgview.layer.borderWidth = 1.0
cellBgview.layer.borderColor = UIColor.clear.cgColor
cellBgview.layer.shadowColor = UIColor.gray.cgColor
cellBgview.layer.shadowRadius = 14.0
cellBgview.layer.shadowOpacity = 0.5
cellBgview.layer.shadowPath = UIBezierPath(roundedRect: cellBgview.bounds, cornerRadius: cellBgview.layer.cornerRadius).cgPath
// In
}
This looks like this
Corner radius not working in this case to make corner radius work I have added following code
cellBgview.clipsToBounds = true
After adding above code it looks this
Note that after adding cellBgview.clipsToBounds = true card elevation and shadow is missing but corner radius appears
How to make card view with corner radius and shadow without image getting clip.
Also tried
cellBgview.layer.masksToBounds = true
but it is not working.
Add another UIView in your cell and add imageView in that UIView...Apply corner radius to imageView. Apply shadow to that UIView holding imageView. add clipsToBounds to imageView not to that UIView because clipsToBounds clip anything outside bounds that can be shadow as well. will solve your issue
add a uiview in your cell, lets call it content view. in this view add another uiview in which you'll add the image view and your bottom labels. So actual hierarchy would look like this
--Cell
---UIView //add shadow on this view, also make its backgroundColor to clear color
---UIView //add corner radius to this view and clipsToBounds = true for this view
---UIImageView
---Bottom Labels / Another UIView / Stack View
this hierarchy will help you to achieve the image round corner only on top and view round corners on bottom content
Try this to achieve Shadow, Corner Radius, Border in one view.
cellBgview.backgroundColor = UIColor.white
let cellBgViewLayer = cellBgview.layer()
cellBgViewLayer?.masksToBounds = false
cellBgViewLayer?.shadowColor = UIColor.lightGray.cgColor
cellBgViewLayer?.shadowOpacity = 1.0
cellBgViewLayer?.shadowRadius = 6.0
cellBgViewLayer?.shadowOffset = CGSize(width: 0, height: 0)
cellBgViewLayer?.shouldRasterize = true
cellBgViewLayer?.cornerRadius = 5.0
cellBgViewLayer?.borderColor = UIColor.lightGray.cgColor
cellBgViewLayer?.borderWidth = 1.0
cellBgViewLayer?.shadowPath = UIBezierPath(rect: cellBgview.bounds).cgPath
I am using IGListKit with my UICollectionView. For now the CollectionView is pretty simple, as it only have one cell per section. That cell contains an inner horizontal UICollectionView as an image slideshow.
As I need some shadowing around my entire sections, I am using Decoration Views, and apply it a border shadow: layer.shadowPath
I noticed something weird, the shadow's opacity changes upon the picture currently displayed in the slideshow. If the picture (or a portion of the picture) is bright, you can see the shadow opacity changing.
I don't know if it is something I can fix.
You can clearly see that if I take a screenshot while swiping pictures in the slideshow, the shadow on the top is darker on one side.
Code for decoration view:
class FeedItemBackgroundShadowView: UICollectionReusableView {
// MARK: Initialization
... Constructors calling setup
// MARK: Setup
override func layoutSubviews() {
super.layoutSubviews()
self.layer.shadowPath = UIBezierPath(roundedRect: self.bounds, cornerRadius: Constants.cornerRadius).cgPath
}
func setup() {
self.layer.cornerRadius = 12.0
self.layer.backgroundColor = UIColor.white.cgColor
self.layer.shadowColor = UIColor.black.cgColor
self.layer.shadowOffset = CGSize(width: 0.0, height: 2.5)
self.layer.shadowRadius = 12.0
self.layer.shadowOpacity = 0.35
}
Rest of the code a simply UICollectionViewCells that embed a UICollectionView
Are you certain it isn't an optical illusion?
I am trying to have the shadow surround a View, but for some reason, the shadow only appears on the top and left side of the View, below is my code and view image. Can someone tell me what I did wrong and how should I change my code, so that the shadow will also appear on the right and bottom? Thank you.
override func awakeFromNib() {
layer.shadowOpacity = 1
layer.shadowRadius = 10
layer.shadowOffset = CGSize(width:1, height: 1)
layer.shadowColor = UIColor.black.cgColor
layer.shadowPath = CGPath(rect: bounds, transform: nil)
}
the view
I'm attempting to create a method to configure buttons in an app. My method performs as expected, but the shadows don't draw. It does not crash.
I've created a UIButton extension with the following method:
func configureWhiteText(withBackgroundColor buttonColor: UIColor?) {
// basic configuration
tintColor = UIColor.white
setTitleColor(UIColor.white, for: .normal)
backgroundColor = buttonColor
// layer stuff
layer.drawsAsynchronously = true
layer.shouldRasterize = true
layer.cornerRadius = 10.0
layer.masksToBounds = false
layer.shadowColor = UIColor.black.cgColor
layer.shadowOffset = CGSize(width: 5.0, height: 5.0)
layer.shadowRadius = 5.0
}
I've tried it within a UITableViewController in viewDidLoad and 'viewWillAppear` without luck:
myButton.configureWhiteText(withBackgroundColor: UIColor.blue)
The buttons are in grouped static cells of UITableViewController. Everything works within the configuration method except the shadow-oriented lines of code. layer.cornerRadius rounds the corner as expected, but none of the other layer-oriented lines draw a shadow.
I've verified layer.masksToBounds = false. I also tried commenting out layer.drawsAsynchronously and layer.shouldRasterize to no avail.
Any further ideas/suggestions are appreciated.
You left out the setting that actually turns on the shadow — the shadowOpacity. Add this line:
layer.shadowOpacity = 0.5
Badda bing badda boom.
You need to set the shadow path, assuming it is in an uibutton extension
self.layer.shadowPath = UIBezierPath(roundedRect: self.bounds, cornerRadius: 20).CGPath
For someone, like me, who might still face the same issue even if layer.shadowOpacity is set to a non-zero value. If your button background color is UIColor.clear, you should change it to other colors, e.g. white color.
I have created a tableviewcell in XIB and used it in tableview using registering the class. I am using autolayout here.
But the problem is when i loading the tableview in viewcontroller the shadow of the view inside the cell is not setting correctly. it exceeds its bounds as i shown in picture with red box.
But when is scrolling the tableview then the shadow of the view is shown correctly as expected.
The code used in cellForRowAtIndexPath is shown below:
let layer = cell.cellContectView.layer
layer.shadowOffset = CGSizeMake(1.0, 1.0)
layer.shadowColor = UIColor.blackColor().CGColor
layer.shadowRadius = 5
layer.shadowOpacity = 0.2
layer.masksToBounds = false
layer.shadowPath = UIBezierPath(rect: layer.bounds).CGPath
Please note: the blurred text is not an issue. Its hidden for security reason
You can try to remove the code for the layer from the cellForRowAtIndexPath and put it in your cell class in:
override func prepareForReuse() {
let layer = self.contentView.layer
layer.shadowOffset = CGSizeZero
layer.shadowColor = UIColor.blackColor().CGColor
layer.shadowRadius = 5
layer.shadowOpacity = 0.2
layer.masksToBounds = false
layer.shadowPath = UIBezierPath(rect: layer.bounds).CGPath
}
Adding a shadow inside UITableViewCell using QuartzCore code - can decrease scroll performance dramatically.
Better to create shadow graphic asset and add it as UIImageView.