1

Here is what I am trying to do:

Original Screen Shot

The screenshot is taken from Iphone:

Taken Screenshot

This is my code:

cell.shadowLayerView.layer.masksToBounds = false
cell.shadowLayerView.layer.shadowOffset = CGSize(width: 0, height: 0)
cell.shadowLayerView.layer.shadowColor = UIColor.black.cgColor
cell.shadowLayerView.layer.shadowOpacity = 0.23
cell.shadowLayerView.layer.shadowRadius = 4
cell.shadowLayerView.layer.shadowPath = UIBezierPath(roundedRect: cell.shadowLayerView.bounds, cornerRadius: 2).cgPath
cell.shadowLayerView.layer.shouldRasterize = true
cell.shadowLayerView.layer.rasterizationScale = UIScreen.main.scale
cell.discriptionLbl.frame.size.width = UIScreen.main.bounds.size.width

This is my tableview xib. In original image show light gray color shadow all side(Top,Bottom,left,Right) but in taken image show all side shadow but why show extra shadow in right side and bottom side.

See Below Image:

Latest Taken Image

Question: How to show same shadow from all side of the view like original image(shadow in light gray color)?

Can someone please explain to me how to solve this , i've tried to solve this issue but no results yet.

Any help would be greatly appreciated.

Thanks in advance.

Sham Dhiman
  • 1,348
  • 1
  • 21
  • 59
  • [this link may help you to get what you want](https://stackoverflow.com/questions/37645408/uitableviewcell-rounded-corners-and-shadow) – Muhammad Umair Gillani Jan 01 '19 at 13:11
  • 2
    Supplying only a link as an answer is discouraged. The link can break if the page moves. Please consider explaining the basics of the answer you are providing since we’re not here to do basic searches that the OP could do themselves. – Magnas Jan 01 '19 at 13:12
  • set the shadow for cell contentView – a.masri Jan 01 '19 at 13:33
  • update question please check and give me solution. – Sham Dhiman Jan 02 '19 at 06:30

2 Answers2

1

See the below code:

mainViewCorner.layer.shadowColor = UIColor.red.cgColor;
mainViewCorner.layer.shadowOffset = CGSize.zero //direction of shadow
mainViewCorner.layer.shadowOpacity = 1.0; // opacity for shadow
mainViewCorner.layer.shadowRadius = 0.0; //amount of shadow to blur
mainViewCorner.layer.cornerRadius = 4.0;

issue with your code was shadowOffset. It defines the direction of your shadow. If you want shadow on all sides - it must have zero value for both width and height.

Mehul Thakkar
  • 12,440
  • 10
  • 52
  • 81
  • @Sam : Have you read my answer, I already explained that, `shadowOffset` defines the direction for your shadow. So, if you want shadow equal in all directions, then `shadowOffset` must be CGSize.zero – Mehul Thakkar Jan 02 '19 at 06:33
  • In what you have tried - please remove `shadowPath`, As you are using shadowPath and shadowOffset both, it will get conflict – Mehul Thakkar Jan 02 '19 at 06:35
  • Ya @Mehul you are right you solve my problem thank you very much please explain when i m trying to CGSize(width: 0.0, height: 0.0) and cgsize.zero is not same ?// – Sham Dhiman Jan 02 '19 at 06:38
  • You can see answer of Dipen in this question(Dont forget to read comments in the code he provided, otherwise it will not work): https://stackoverflow.com/questions/36173052/ios-how-to-make-a-shadow-for-uiview-on-4-side-top-right-bottom-and-left, – Mehul Thakkar Jan 02 '19 at 06:38
  • yes both cgsize are same, user whichever looks better to you. both are same. @Sam – Mehul Thakkar Jan 02 '19 at 06:39
  • but when i m trying to set CGSize(width: 0.0, height: 0.0) they show not properly but when i try to CGSize.zero then solved my problem ?? – Sham Dhiman Jan 02 '19 at 06:39
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/186027/discussion-between-sam-and-mehul-thakkar). – Sham Dhiman Jan 02 '19 at 06:40
  • This looks strange... @Sam, In my code, i always use the code that i pasted, not cgsize.zero and it is working in my case. Probably you missed something – Mehul Thakkar Jan 02 '19 at 06:41
0

try it

   myView.layer.cornerRadius = 10
   let shadowPath = UIBezierPath(roundedRect: myView.bounds, cornerRadius: 10)

    myView.layer.masksToBounds = false
    myView.layer.shadowColor = UIColor.gray.cgColor
    myView.layer.shadowOffset = CGSize(width: 0, height: 2)
    myView.layer.shadowOpacity = 0.5

Or you can use Sketch Shadow

extension CALayer {
    func applySketchShadow(
        color: UIColor = .black,
        alpha: Float = 0.5,
        x: CGFloat = 0,
        y: CGFloat = 2,
        blur: CGFloat = 4,
        spread: CGFloat = 0)
    {
        shadowColor = color.cgColor
        shadowOpacity = alpha
        shadowOffset = CGSize(width: x, height: y)
        shadowRadius = blur / 2.0
        if spread == 0 {
            shadowPath = nil
        } else {
            let dx = -spread
            let rect = bounds.insetBy(dx: dx, dy: dx)
            shadowPath = UIBezierPath(rect: rect).cgPath
        }
    }
}

apply Sketch Shadow

 myView.layer.cornerRadius = 10
        let shadowPath = UIBezierPath(roundedRect: myView.bounds, cornerRadius: 10)

   myView.layer.applySketchShadow(
            color: .black, 
            alpha: 0.5, 
            x: CGFloat(0), 
            y: CGFloat(10), 
            blur: 20, 
            spread: 0)
        myView.layer.shadowPath = shadowPath.cgPath
a.masri
  • 2,439
  • 1
  • 14
  • 32