本文介绍了UIView阴影和InterfaceBuilder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用CALayer将阴影添加到UI(图像)视图中。
以下代码通常可以正常使用

I want to add a drop shadow using CALayer to a UI(Image)View.The following code works generally fine

previewImage.layer.shadowColor = [[UIColor blackColor] CGColor];
previewImage.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
previewImage.layer.shadowOpacity = 1.0f;
previewImage.layer.shadowRadius = 8.0f;

但是,这只适用于我以编程方式创建该视图并将其作为子视图添加到我的主视图中。当在InterfaceBuilder中设置该视图并将其定义为IBOutlet UIImageView时,这不起作用。没有阴影出现。
那么我在这里缺少什么?

However, this only works if I create that view programmatically and add it as a subview to my main view. When that view is set up in InterfaceBuilder and defined as an IBOutlet UIImageView this does NOT work. No shadow appears.So what am I missing here?

推荐答案

我不确定问题是什么 - 确保你的 UIImageView clipsToBounds 属性设置为。通过引用您的IBOutlet从nib文件加载后,您可以在 viewDidLoad 中执行此操作。您不应该将其包装在另一个视图中。




修改

I'm not sure what the issue is - ensure your UIImageView's clipsToBounds property is set to NO. You can do this in viewDidLoad after loading from the nib file by referencing your IBOutlet. You shouldn't need to wrap it in another view.

Edit

根据您需要的图像使用方面填充缩放,您可以使用 UIImageView 的基础图层的 contentsRect 属性来模拟效果内容剪辑。 contentsRect 是图层内容(在本例中为图像)的单位坐标空间中的矩形,用于定义应绘制内容的子矩形。

In light of you needing your image to be scaled using aspect fill, you can use the contentsRect property of the UIImageView's underlying layer to 'simulate' the effect of the contents clipping. contentsRect is rectangle in the unit coordinate space of the layer's content (in this case your image) that defines a sub-rectangle of the contents that should be drawn.

通过一些数学运算,我们可以通过比较图像视图大小和图像大小来找到这个矩形(考虑到方面填充缩放):

With a little bit of maths, we can find this rectangle by comparing the image view size with the image size (accounting for the aspect fill scaling):

CGSize imageViewSize = previewImage.size;
CGSize imageSize = previewImage.image.size;

// Find the scaling required for the image to fit the image view (as for aspect fill).
CGFloat imageWidthScale = fabsf(imageViewSize.width / imageSize.width);
CGFloat imageHeightScale = fabsf(imageViewSize.height / imageSize.height);
CGFloat imageScale = (imageWidthScale > imageHeightScale) ? imageWidthScale : imageHeightScale;

// Determine the new image size, after scaling.
CGSize scaledImageSize = CGSizeApplyAffineTransform(imageSize, CGAffineTransformMakeScale(imageScale, imageScale));

// Set the layer's contentsRect property in order to 'clip' the image to the image view's bounds.
previewImage.layer.contentsRect = CGRectMake(((scaledImageSize.width - imageViewSize.width) / 2.0f) / scaledImageSize.width,
                                             ((scaledImageSize.height - imageViewSize.height) / 2.0f) / scaledImageSize.height,
                                             imageViewSize.width / scaledImageSize.width,
                                             imageViewSize.height / scaledImageSize.height);

这样做,你可以留下 clipsToBounds 设置到图像视图的,但图像仍会显示为剪裁。如果您需要更改图像视图大小,可以方便地将此代码包装到以 UIImageView 作为参数的方法中。

Doing this, you can leave clipsToBounds set to NO for your image view, but the image will still appear clipped. If you need to change the image view size at all, it might be convenient to wrap this code up into a method that takes a UIImageView as a parameter.

我希望这会有所帮助。

这篇关于UIView阴影和InterfaceBuilder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-10 20:02