D
D
Darkness2020-02-18 17:07:52
iOS
Darkness, 2020-02-18 17:07:52

How to add shadow to custom cell in UICollectionView if masksToBounds breaks UI?

Problem:
It consists in the fact that I need to add a shadow to the custom cells in the UICollectionView, if
cell.layer.masksToBounds = falsethe shadow is added, but the UI breaks (screen 1)
C masksToBounds = true- there is no shadow, but the whole UI is correct (screen 2)

Implementation:
I did not add constraints
I myself am trying to give shadow when defining cell. in the dataSource delegate Like
this:

// cell.layer.masksToBounds = false // or true
cell.layer.shadowColor = #colorLiteral(red: 0.2549019754, green: 0.2745098174, blue: 0.3019607961, alpha: 1) // grey
 cell.layer.shadowOffset = CGSize(width: 2.0, height: 4.0)
 cell.layer.shadowRadius = 2.0
 cell.layer.shadowOpacity = 1.0


My cell itself is in a .xib file, I also specified my own dimensions for the cell
Question:
How to set a shadow for a cell without breaking the UI? Tried in the controller for xib did not work.

5e4bf10ed236d506009351.png
5e4bf16129298740064674.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Vorobei, 2020-02-18
@AntonBrock

You need to make a container-mask . Create a view with a cell size, put a mask on it. All subviews to this container now apply mask rules to them .
The shadow is applied to the cell itself - it will not be cut by the container's mask.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question