UIView with rounded corners and drop shadow?

前端 未结 30 2616
一整个雨季
一整个雨季 2020-11-22 08:00

I’ve been working on an application for a couple of years and received a simple design request: Round the corners on a UIView and add a drop shadow.To do as given below.

30条回答
  •  情歌与酒
    2020-11-22 08:27

    Swift 4 Solution for making UICollectionViewCell round and adding Shadows, without any extensions and complications :)

    Note: For simple views e.g Buttons. See the @suragch's Answer in this post. https://stackoverflow.com/a/34984063/7698092. Tested successfully for buttons

    In case if any one still struggling to round the corners and add shadows at the same time. Although this solution works with UICollectionViewCell, it can be generalized to any view.

    This technique worked for me without making any extensions and all the complicated stuff. I am working with storyBoard.

    Technique

    You must add a UIView (lets say it "containerView") inside your UICollectionViewCell in storyBoard and add all the required views (buttons, images etc) inside this containerView. See the Screenshot.

    Connect the outlet for containerView. Add following lines of code in CellforItemAtIndexPath delegate function.

    //adds shadow to the layer of cell
    
    cell.layer.cornerRadius = 3.0
        cell.layer.masksToBounds = false
        cell.layer.shadowColor = UIColor.black.cgColor
        cell.layer.shadowOffset = CGSize(width: 0, height: 0)
        cell.layer.shadowOpacity = 0.6
    
    //makes the cell round 
    
    let containerView = cell.containerView!
        containerView.layer.cornerRadius = 8
        containerView.clipsToBounds = true
    

    Output

    See the simulator Screenshot

提交回复
热议问题