简体   繁体   English

将UIImageView裁剪为父母的边界

[英]Crop UIImageView to bounds of parents

I have a UIImageView within another UIView. 我在另一个UIView中有一个UIImageView。 The UIImageView is slightly taller than the UIView. UIImageView比UIView略高。 Though, I want the UIImageView to only be viewable within the UIView. 虽然,我希望UIImageView仅在UIView中可见。 Any part of the UIImageView outside of the UIView should not be seen. 不应看到UIView之外的UIImageView的任何部分。

I'm using a UITableView and inside the UITableViewCell will be that UIView that gives it a "card" look. 我使用的是UITableView,并且在UITableViewCell内将是为其提供“卡片”外观的UIView。 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. UIView是单元格内的内容。

From looking only, I had to set the clipToBounds to true from the UIView. 仅从外观来看,我必须从UIView中将clipToBounds设置为true 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. UIView和“关于我们” UIView可以正确裁剪图像。 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. 如您所见,图像在UIView上方和下方流血。

FYI the images have an opacity of 0.35 . 仅供参考,图像的不透明度为0.35

How do I get the images to properly crop to the UIView border? 如何使图像正确裁剪到UIView边框?

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. 在您的情况下,它非常灵活,因此uitableViewCell可以在imageview中获取图像的高度。

Set UIImageView attribute, clipsToBounds = true 设置UIImageView属性, clipsToBounds = true

not just the parent UIView. 不只是父级UIView。 They have different attributes even the UIImageView is a child of your UIView. 它们具有不同的属性,即使UIImageView是UIView的子级也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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