skip to Main Content

I am trying make some shadow effect on my logo image but the effect is not showing off..

func setUpLogo(){
    view.addSubview(logoView)
    logoView.translatesAutoresizingMaskIntoConstraints = false
    logoView.topAnchor.constraint(equalTo: view.topAnchor,constant: 220).isActive = true
    logoView.widthAnchor.constraint(equalToConstant: 140).isActive = true
    logoView.heightAnchor.constraint(equalToConstant: 190).isActive = true
    logoView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive  = true
    logoView.image = UIImage(named: "logoCharity")
    logoView.layer.cornerRadius = 11
    logoView.clipsToBounds = true
    logoView.layer.shadowColor = UIColor.red.cgColor
    logoView.layer.shadowOpacity = 1
    logoView.layer.shadowOffset = CGSize(width: 0, height: 0)
    logoView.layer.shadowRadius = 10
}

2

Answers


  1. Can you try this one or change into logoView.clipsToBounds = false

    let imageView = UIImageView(image: UIImage(named: "your_image"))
    imageView.layer.cornerRadius = 8.0
    imageView.layer.masksToBounds = true
    imageView.layer.shadowColor = UIColor.black.cgColor
    imageView.layer.shadowOffset = CGSize(width: 0, height: 2)
    imageView.layer.shadowOpacity = 0.5
    imageView.layer.shadowRadius = 4
    
    Login or Signup to reply.
  2. Basically you cannot have both clipping corners and shadow overlapping the view bounds.

    To achieve that, you need to have two views – one for clipping and one for the shadow.

    let container = UIView()
    view.addSubview(container)
    container.addSubview(logoView)
    
    container.backgroundColor = .clear
    container.clipsToBounds = false
    container.layer.cornerRadius = 11
    container.layer.shadowColor = UIColor.red.cgColor
    container.layer.shadowOpacity = 1
    container.layer.shadowOffset = .zero
    container.layer.shadowRadius = 10
    
    logoView.image = UIImage(named: "logoCharity")
    logoView.clipsToBounds = true
    logoView.layer.cornerRadius = 11
    
    container.translatesAutoresizingMaskIntoConstraints = false
    container.topAnchor.constraint(equalTo: view.topAnchor,constant: 220).isActive = true
    container.widthAnchor.constraint(equalToConstant: 140).isActive = true
    container.heightAnchor.constraint(equalToConstant: 190).isActive = true
    container.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive  = true
    
    logoView.translatesAutoresizingMaskIntoConstraints = false
    logoView.leftAnchor.constraint(equalTo: container.leftAnchor).isActive = true
    logoView.rightAnchor.constraint(equalTo: container.rightAnchor).isActive = true
    logoView.topAnchor.constraint(equalTo: container.topAnchor).isActive = true
    logoView.bottomAnchor.constraint(equalTo: container.bottomAnchor).isActive = true
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search