Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add corner radius and shadow to a UIView in iOS

enter image description hereAfter autolayout (Constraints) How to make corner radius and shadow of the view ( even the view have some subview also ) and which method to write the code. If i write the code in viewdidload , viewDidAppear that i cant get the exact corner radius and shadow ?

This is my code which I do in viewWillLayoutSubview , I can get the corner radius but I cannot get shadow effects. If I remove view.layer.masksToBounds = true I can get shadow but its not corner only for base view not for its subviews ..

view.layer.cornerRadius = 10
view.layer.masksToBounds = true

view.layer.borderWidth = 1.0
view.layer.borderColor = UIColor.black.cgColor

view.layer.shadowColor = UIColor.black.cgColor
view.layer.shadowOffset = CGSize(width: 3, height: 3)
view.layer.shadowOpacity = 0.7
view.layer.shadowRadius = 4.0
like image 214
user8343375 Avatar asked Jan 03 '23 02:01

user8343375


1 Answers

    viewShadow.layer.cornerRadius = 12
    viewShadow.layer.masksToBounds = true;

    viewShadow.backgroundColor = UIColor.white
    viewShadow.layer.shadowColor = UIColor.lightGray.cgColor
    viewShadow.layer.shadowOpacity = 0.8
    viewShadow.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
    viewShadow.layer.shadowRadius = 6.0
    viewShadow.layer.masksToBounds = false

enter image description here

like image 170
Tajinder singh Avatar answered Jan 05 '23 16:01

Tajinder singh