简体   繁体   English

UIImageView蒙版和底部对齐

[英]UIImageView masks and bottom alignment

I am working on an app that has TableViewCells with varying heights. 我正在开发一个具有可变高度的TableViewCells的应用程序。 I am placing a UIImageView into them and each will use the same size image (the same size of the largest cell), however I need to mask the excess in the smaller cells (keeping the bottom, not the top). 我将UIImageView放入其中,每个图像都将使用相同大小的图像(最大单元格的大小相同),但是我需要掩盖较小单元格中的多余图像(保持底部,而不是顶部)。

To be more specific, I have 3 different cell heights, 112, 104 and 88. The images will all be 112 tall and I want the images to have the tops cut off on the smaller cells. 更具体地说,我有3个不同的像元高度,分别为112、104和88。图像将全部为112高,我希望图像在较小的像元上切掉顶部。 Im pretty sure the answer lies within the bounds, frame and center attributes of a UIImageView, but I cant figure out exactly what I should be doing. 我很确定答案在UIImageView的边界,框架和中心属性之内,但我无法弄清楚应该怎么做。

You will need to set the frame of your image view such that the bottom of the image view is aligned with the bottom of your cell. 您将需要设置图像视图的框架,以使图像视图的底部与单元格的底部对齐。

You will also need to ensure the contentview of you cell is clipping content to its bounds (setClipToBounds to yes). 您还需要确保单元格的contentview将内容裁剪到其边界(setClipToBounds为yes)。

If I had to do this I would subclass the UITabelViewCell class and implement the layoutSubviews method. 如果必须这样做,则可以将UITabelViewCell类子类化,并实现layoutSubviews方法。 In your implementation don't forget to call super first so the content view has the right size (also if you go in editing mode). 在您的实现中,不要忘了先调用super,这样内容视图的大小就正确了(即使在编辑模式下也是如此)。 Then use the content view bounds and place your content accordingly. 然后使用内容视图范围并相应地放置您的内容。

将图像视图添加到单元格时,只要将UIImageView底部放置在框架中,它就会自动切除顶部,这应该是您想要的。

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

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