Altynbek Tanbayev
Altynbek Tanbayev

Reputation: 37

Shadow at top side of UIView only

How can I remove shadow from bottom side of my view? This is code for my shadowView:

class ShadowViewWhite: UIView {

init() {
    super.init(frame: .zero)
    backgroundColor = .white
    layer.shadowColor = UIColor(red: 0.5, green: 0.5, blue: 0.65, alpha: 0.9).cgColor
    layer.shadowOpacity = 1
    layer.shadowRadius = 40
    layer.shadowOffset = CGSize(width: 0, height: -12)
}

required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

}

screenShot

That's how it looks so far. I need shadow to be only at the top side of my view.

Upvotes: 1

Views: 1097

Answers (2)

Piavio
Piavio

Reputation: 84

A combination of reducing the shadowRadius and shadowOffset should help you. I have attached my code below which should help you.

class ShadowViewWhite: UIView {
    
    override init(frame:CGRect) {
        super.init(frame: frame)
        backgroundColor = .white
        layer.shadowColor = UIColor(red: 0.5, green: 0.5, blue: 0.65, alpha: 0.9).cgColor
        layer.shadowOpacity = 1
        layer.shadowRadius = 20
        layer.shadowOffset = CGSize(width: 0, height: -40)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

This code will produce the following image:

Upvotes: 2

Anjali Aggarwal
Anjali Aggarwal

Reputation: 629

Using lower values of shadow radius can help. You can try using shadowRadius = 20.

Upvotes: 0

Related Questions