Search code examples
iosobjective-cxcode6cliptobounds

Clip to bounds of a different UIImageView


I have two UIImageViews, imageView1 and imageView2. I've set

self.imageView1.clipsToBounds = YES;

but I was wondering if it is possible to set imageView1 to clipToBounds of imageView2?


Solution

  • Not easily. The "right" way to do this would be to use a clipping path on the imageView1's layer. However, you can get the same effect much more easily as follows.

    Put imageView1 centered inside a container UIView, and turn that view's clipsToBounds on. Then:

    • If you're using Auto Layout, constrain the container view's size to be equal to the size of imageView2.

    • If you're not using Auto Layout, set the bounds of the container view to the size of imageView2 whenever you calculate layout.