Moodz
Moodz

Reputation: 77

The shadow added to the view created is not appearing

I am adding programatically a UIView, and setting its contsraints using NSLayoutConstraint as below, yet teh shadow is not being added.

For the shadow i am using SwifterSwift .addShadow()

The result i get

UiView:

    lazy var alertViewNew: UIView = {
        let view = UIView()
        view.layer.zPosition = 1
        view.cornerRadius = 20
        view.addShadow(ofColor: .lightGray, radius: 3, offset: .zero, opacity: 0.3)
        view.translatesAutoresizingMaskIntoConstraints = false
        
        return alertView
    }()

Adding the Constaraints

func setUpAlertView() {
                
        [alertViewNew].forEach {
            (view.addSubview($0))
        }

        NSLayoutConstraint.activate([
        
            alertViewNew.centerYAnchor.constraint(equalTo: view.centerYAnchor),
            alertViewNew.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            alertViewNew.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 10),
            alertViewNew.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10),
            
            titleLabel.leadingAnchor.constraint(equalTo: alertViewNew.leadingAnchor, constant: 20),
            titleLabel.trailingAnchor.constraint(equalTo: alertViewNew.trailingAnchor, constant: -20),
            titleLabel.topAnchor.constraint(equalTo: alertViewNew.topAnchor, constant: 20),
            
            descriptionLabel.leadingAnchor.constraint(equalTo: alertViewNew.leadingAnchor, constant: 20),
            descriptionLabel.trailingAnchor.constraint(equalTo: alertViewNew.trailingAnchor, constant: -20),
            descriptionLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 20),
            
            updateButton.topAnchor.constraint(equalTo: descriptionLabel.bottomAnchor, constant: 5),
            updateButton.trailingAnchor.constraint(equalTo: alertViewNew.trailingAnchor, constant: -20),
            updateButton.widthAnchor.constraint(equalToConstant: 65),
            updateButton.bottomAnchor.constraint(equalTo: alertViewNew.bottomAnchor, constant: -20),
        ])
    }

AddShadow by Swifter Swift

    func addShadow(ofColor color: UIColor = UIColor(red: 0.07, green: 0.47, blue: 0.57, alpha: 1.0), radius: CGFloat = 3, offset: CGSize = .zero, opacity: Float = 0.5) {
        layer.shadowColor = color.cgColor
        layer.shadowOffset = offset
        layer.shadowRadius = radius
        layer.shadowOpacity = opacity
        layer.masksToBounds = false
    }

Things i tried to fix the issue

Setting mask to bound to true

setting is opaque to true

and some other trials found on stackoverflow

None of this worked

Upvotes: 0

Views: 52

Answers (1)

DonMag
DonMag

Reputation: 77423

It's a bit difficult to help, because the code you've shown is incomplete (and has errors, as written).

For example, I assume your func addShadow is in a UIView extension like this:

extension UIView {
    func addShadow(ofColor color: UIColor = UIColor(red: 0.07, green: 0.47, blue: 0.57, alpha: 1.0), radius: CGFloat = 3, offset: CGSize = .zero, opacity: Float = 0.5) {
        layer.shadowColor = color.cgColor
        layer.shadowOffset = offset
        layer.shadowRadius = radius
        layer.shadowOpacity = opacity
        
        // no need for this
        //layer.masksToBounds = false
    }
}

Next, your lazy var alertViewNew:

lazy var alertViewNew: UIView = {
    let view = UIView()
    
    // no logical reason for this
    //view.layer.zPosition = 1
    
    // .cornerRadius is not a property of `UIView`
    //view.cornerRadius = 20
    
    // assuming this is in a UIView extension
    view.addShadow(ofColor: .lightGray, radius: 3, offset: .zero, opacity: 0.3)
    
    view.translatesAutoresizingMaskIntoConstraints = false

    // no such thing as alertView
    //return alertView
    
    return view
}()

However... if we assume you have code that actually works (labels are defined and created somewhere, subviews are added correctly, etc), the most likely reason you're not seeing the shadow is because your alertViewNew probably has a clear background. If it is clear, there is nothing there to "cast a shadow."

Try setting alertViewNew.backgroundColor = .white and see if that fixes the problem.

Or, try this working example:

class CustomAlertTestVC: UIViewController {
    
    lazy var alertViewNew: UIView = {
        let view = UIView()
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .white
        
        view.addSubview(alertViewNew)
        setUpAlertView()

    }
    
    func setUpAlertView() {

        let titleLabel = UILabel()
        let descriptionLabel = UILabel()
        let updateButton = UIButton()

        titleLabel.font = .boldSystemFont(ofSize: 16.0)
        titleLabel.text = "New Version Available"
        
        descriptionLabel.font = .systemFont(ofSize: 16.0)
        descriptionLabel.numberOfLines = 0
        descriptionLabel.text = "Please, Update application to the new version to continue."
        
        updateButton.setTitle("UPDATE", for: [])
        updateButton.setTitleColor(.systemBlue, for: [])

        [titleLabel, descriptionLabel, updateButton].forEach {
            $0.translatesAutoresizingMaskIntoConstraints = false
            alertViewNew.addSubview($0)
        }
        
        NSLayoutConstraint.activate([
            
            alertViewNew.centerYAnchor.constraint(equalTo: view.centerYAnchor),
            // no need for centerX since we're adding leading and trailing constraints
            //alertViewNew.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            alertViewNew.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 10),
            alertViewNew.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10),
            
            titleLabel.leadingAnchor.constraint(equalTo: alertViewNew.leadingAnchor, constant: 20),
            titleLabel.trailingAnchor.constraint(equalTo: alertViewNew.trailingAnchor, constant: -20),
            titleLabel.topAnchor.constraint(equalTo: alertViewNew.topAnchor, constant: 20),
            
            descriptionLabel.leadingAnchor.constraint(equalTo: alertViewNew.leadingAnchor, constant: 20),
            descriptionLabel.trailingAnchor.constraint(equalTo: alertViewNew.trailingAnchor, constant: -20),
            descriptionLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 20),
            
            updateButton.topAnchor.constraint(equalTo: descriptionLabel.bottomAnchor, constant: 5),
            updateButton.trailingAnchor.constraint(equalTo: alertViewNew.trailingAnchor, constant: -20),
            // really no need for width constraint
            //updateButton.widthAnchor.constraint(equalToConstant: 65),
            updateButton.bottomAnchor.constraint(equalTo: alertViewNew.bottomAnchor, constant: -20),
            
        ])
        
        alertViewNew.layer.shadowColor = UIColor.lightGray.cgColor
        alertViewNew.layer.shadowOffset = .zero
        alertViewNew.layer.shadowRadius = 3.0
        alertViewNew.layer.shadowOpacity = 0.3
        alertViewNew.layer.cornerRadius = 20.0
        
        // to get the view's layer to "cast a shadow"
        
        // either set the view's backgroundColor
        alertViewNew.backgroundColor = .white
        
        // or, set the layer's backgroundColor
        //alertViewNew.layer.backgroundColor = UIColor.white.cgColor

    }

}

Output:

enter image description here

Upvotes: 1

Related Questions