I am trying to give shadow to a UIStackView. My stackview is in a prototypecell having some labels and buttons. Firstly, I have made a reference of stackview with name UIViewBank in a class that is binded to my custom cell:
class CustChatBankCell: UITableViewCell {
    @IBOutlet weak var UIViewBank: UIStackView!
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        // Configure the view for the selected state
    }
}
And in my Controller I am using the extension in:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:CustChatBankCell = self.tbChat.dequeueReusableCell(withIdentifier: "CustomBankCell") as! CustChatBankCell
                    cell.UIViewBank.dropShadow()
                    return cell
                }
}
Extension code:
extension UIStackView {
    func dropShadow(scale: Bool = true) {
        self.layer.masksToBounds = false
        self.layer.shadowColor = UIColor.black.cgColor
        self.layer.shadowOpacity = 0.5
        self.layer.shadowOffset = CGSize(width: -1, height: 501)
        self.layer.shadowRadius = 1
        self.layer.shadowPath = UIBezierPath(rect: self.bounds).cgPath
        self.layer.shouldRasterize = true
        self.layer.rasterizationScale = scale ? UIScreen.main.scale : 1
    }
}
But shadow is not appearing.
shadowOpacity sets how transparent the shadow is, where 0 is invisible and 1 is as strong as possible. shadowOffset sets how far away from the view the shadow should be, to give a 3D offset effect.
You can't do this because UIStackView is a non-drawing view, meaning that drawRect() is never called and its background color is ignored. Consider placing the UIStackView inside another UIView and giving that view a background color.
From Apple Docs
The UIStackView is a nonrendering subclass of UIView ; that is, it does not provide any user interface of its own. Instead, it just manages the position and size of its arranged views. As a result, some properties (like backgroundColor ) have no effect on the stack view. Similarly, you cannot override layerClass , draw(:) , or draw(:in:).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With