0

Here corner is working but shadow is not work in iOS 9 and 10

and iOS 11 both are working fine

 viewBlueMe.chatCellDesign([.topLeft,.bottomLeft,.topRight], [.layerMinXMinYCorner,.layerMinXMaxYCorner,.layerMaxXMinYCorner], radius: 5)

extenstion :

    extension UIView {
    func chatCellDesign(_ corners:UIRectCorner,_ cormerMask:CACornerMask, radius: CGFloat) {

        if #available(iOS 11.0, *){
            self.clipsToBounds = false
            self.layer.cornerRadius = radius
            self.layer.maskedCorners = cormerMask

            self.layer.shadowColor = UIColor.gray.cgColor
            self.layer.shadowOpacity = 1
            self.layer.shadowOffset = CGSize.zero
            self.layer.shadowRadius = 5
        }else{
            let rectShape = CAShapeLayer()
            rectShape.bounds = self.frame
            rectShape.position = self.center
            rectShape.path = UIBezierPath(roundedRect: self.bounds,    byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius)).cgPath
            self.layer.mask = rectShape

            // FIXME: THIS CODE IS NOT WORK
            self.layer.shadowColor = UIColor.gray.cgColor
            self.layer.shadowOpacity = 1
            self.layer.shadowOffset = CGSize.zero
            self.layer.shadowRadius = 5


        }
    }
}

i need this type of output in iOS 9 and 10 also this is iOS11

Thanks in advance enter image description here

Harshil Kotecha
  • 2,846
  • 4
  • 27
  • 41

1 Answers1

2

Create one outerview and place your UILabel inside it, and please find below details,

outerView.layer.cornerRadius = 5.0
outerView.layer.shadowColor = UIColor.black.cgColor
outerView.layer.shadowOffset = CGSize.zero
outerView.layer.shadowOpacity = 0.2
outerView.layer.shadowRadius = 5.0

UPDATE

See below image,

enter image description here

and this is IBOutlets

@IBOutlet weak var tmpView: UIView!
@IBOutlet weak var innerView: UIView!

You can do this way,

innerView.roundCorners([.topLeft, .topRight, .bottomLeft], radius: 5)
tmpView.layer.shadowColor = UIColor.black.cgColor
tmpView.layer.shadowOffset = CGSize.zero
tmpView.layer.shadowOpacity = 0.2
tmpView.layer.shadowRadius = 10.0

Find UIView extension here,

extension UIView {
    func roundCorners(_ corners: UIRectCorner, radius: CGFloat) {
        let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
        let mask = CAShapeLayer()
        mask.path = path.cgPath
        self.layer.mask = mask
    }
}

this will give you below output,

enter image description here

FYI. Play around shadow property for exact output you want.

PPL
  • 6,357
  • 1
  • 11
  • 30