-1

I'm trying to accomplish a shadow similar to this one enter image description here

The code I try to accomplish the above with is the following, but it doesn't quite nail it.

mainView.layer.shadowOpacity = 0.2
mainView.layer.shadowOffset = .zero
mainView.layer.shadowRadius = 3
mainView.layer.shadowColor = UIColor.darkGray.cgColor
mainView.layer.masksToBounds = false

Any idea how to replicate the exact shadow of the image?

Recusiwe
  • 1,594
  • 4
  • 31
  • 54

3 Answers3

0

Increase shadowOpacity:

    mainView.layer.cornerRadius = 3
    mainView.layer.shadowOpacity = 0.8
    mainView.layer.shadowOffset = CGSize(width: 2, height: 2)
    mainView.layer.shadowRadius = 3
    mainView.layer.shadowColor = UIColor.red.cgColor
    mainView.layer.masksToBounds = false
Nguyen Hoan
  • 1,583
  • 1
  • 11
  • 18
0

please use this simple extension

extension UIView {
        func addShadow() {
            self.layer.masksToBounds = false
            self.layer.shadowColor = UIColor.black.cgColor
            self.layer.shadowOpacity = 0.4
            self.layer.shadowOffset = CGSize(width: 1, height: 2)
            self.layer.shadowRadius = 3
            self.layer.cornerRadius = 15
        }
     }
midhun p
  • 1,987
  • 18
  • 24
0

Don't forget to add background color for the view.

mainView.backgroundColor = .white
mainView.layer.cornerRadius = 5
mainView.layer.shadowOpacity = 0.3
mainView.layer.shadowOffset = CGSize(width: 0, height: 0)
mainView.layer.shadowRadius = 3
mainView.layer.shadowColor = UIColor.darkGray.cgColor
mainView.layer.masksToBounds = false

Hope it helps

George
  • 3,600
  • 2
  • 26
  • 36