简体   繁体   中英

Crop UIImageView to bounds of parents

I have a UIImageView within another UIView. The UIImageView is slightly taller than the UIView. Though, I want the UIImageView to only be viewable within the UIView. Any part of the UIImageView outside of the UIView should not be seen.

I'm using a UITableView and inside the UITableViewCell will be that UIView that gives it a "card" look. As you can see below with the screenshots, the colors (red, blue, purple, green, and orange) is the background color for the cells. The UIView is the content inside the cell.

From looking only, I had to set the clipToBounds to true from the UIView. I did that, and the sample view from storyboard seemed to work.

在此处输入图片说明

The images inside the "What is truth?" UIView and "About Us" UIView crop the image correctly. Now, when I run this in a simulator, it doesn't crop the images.

在此处输入图片说明

As you can see the images bleed above and below the UIView.

FYI the images have an opacity of 0.35 .

How do I get the images to properly crop to the UIView border?

If you are using constraints then add height constraints to image view and then try.

In your case, its flexible, so uitableViewCell taking the height of image inside imageview.

Set UIImageView attribute, clipsToBounds = true

not just the parent UIView. They have different attributes even the UIImageView is a child of your UIView.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM