Asadullah Ali
Asadullah Ali

Reputation: 1064

How can we set a UIView as a mask of another UIView

I have

  1. gradientView it's just a gradient view I have
  2. a view containerView with an anImageView and aUILabel

Now when I try to

gradientView.mask = containerView

it does not show anything 😕 however when I do

gradientView.mask = anImageView

or

gradientView.mask = aUILabel

It apply mask nicely. Is there a way I can achieve

gradientView.mask = containerView

Upvotes: 1

Views: 323

Answers (1)

Milan Nosáľ
Milan Nosáľ

Reputation: 19765

UIImageView and UILabel both have intrinsic content size, but not UIView. Mask of a view has to have a frame set properly. Make sure that the containerView.frame is set:

containerView.frame = gradientView.bounds

Also, make sure that containerView.backgroundColor is set to UIColor.clear.

Upvotes: 1

Related Questions