Skip to content
Kevin Leong edited this page Nov 15, 2016 · 1 revision

Shadows on a UIView

With rounded corners

import UIKit

class View: UIView {
    // bodyView is a subview of shadowView in IB.
    @IBOutlet weak var shadowView: UIView!
    @IBOutlet weak var bodyView: UIView!

    override func awakeFromNib() {
        super.awakeFromNib()
        setupViews()
    }

    // MARK: - Setup Views

    private func setupViews() {
        setupShadowView()
        setupBodyView()
    }

    private func setupShadowView() {
        shadowView.backgroundColor = UIColor.clear
        shadowView.layer.shadowOpacity = 0.8
        shadowView.layer.shadowColor = UIColor.black.cgColor
        shadowView.layer.shadowRadius = 1.5 // Blur
        shadowView.layer.shadowOffset = CGSize(width: 2, height: 2) // Spread
    }

    private func setupBodyView() {
        bodyView.layer.cornerRadius = 15.0
        bodyView.clipsToBounds = true
    }
}

References

Clone this wiki locally