I have added two views inside a stack view. I am having that stack view inside my tableViewCell. Now my stackView is placed in the centre of the content view with width and height. I want to apply gradient color inside my view which is inside my stackView .
Just look at the view below In this case I am giving view a background colour of red and green which I don’t want. I want to apply gradient of four colours to this views.
enter image description here
2
Answers
Create GradientLayer on your tableViewCell(collectionViewCell)
1. Create
CAGradientLayer
Instance on yourtableViewCell
2. Set the layer frame to view's bounds from
layoutSublayers(of:)
If you set your layer's frame some other place, frame becomes weird.
3. Add GradientLayer on the View.
Here is my UIView extension for adding gradient layer.
* Option 1 - Add it fromlayoutSublayers(of:)
* Option 2 - Add it fromcellForRowAt
For example, if we have
cellForRowAt
and like this,Now, we can handle gradientLayer from
configureCell(content:)
.Here is the code of a simple progress bar that I have written in Swift:
This progress bar supports multiple colors.
Simply replace your progress bar logic with the one I posted above (or add the logic for the triangle that is shown below the progress bar).
Preview: