Koh
Koh

Reputation: 2897

Swift: Shadow not showing when view is added to window

I am attempting to cast a shadow to my customView but it is not showing. This customView is added to the window using window?.addSubview(customView).

Implementation so far:

//CustomView setup
lazy var customView: UIView = {
    let v = UIView()
    v.translatesAutoresizingMaskIntoConstraints = false
    v.layer.cornerRadius = 8
    v.layer.shadowColor = UIColor.darkGray.cgColor
    v.layer.shadowOffset = CGSize(width: 0, height: 10)
    v.layer.shadowOpacity = 10.5
    v.layer.shadowRadius = 15.0
    v.layer.masksToBounds = true
    return v
}()

//Adding view to window
window?.addSubview(customView)

NSLayoutConstraint.activate([
    customView.leadingAnchor.constraint(equalTo: window!.leadingAnchor),
    customView.trailingAnchor.constraint(equalTo: window!.trailingAnchor),
    customView.heightAnchor.constraint(equalTo: window!.heightAnchor, multiplier: 1),
    customView.topAnchor.constraint(equalTo: window!.safeAreaLayoutGuide.bottomAnchor, constant: -100)
        ])

I have followed advice from this post and this post, but somehow it doesn't show up for views added to window.

Upvotes: 2

Views: 317

Answers (1)

dalton_c
dalton_c

Reputation: 7171

It's because of this line:

v.layer.masksToBounds = true

If you want shadow and corner rounding, I'd suggest using two layers, one that has the shadow and masksToBounds = false, and another one which is a child of the first and has corner rounding + masksToBounds = true

Upvotes: 1

Related Questions