无法在图像视图延伸到其边界的图层上设置cornerRadius和shadow? [英] Can't set cornerRadius AND shadow on layer that has an image view stretched to its bounds?

查看:69
本文介绍了无法在图像视图延伸到其边界的图层上设置cornerRadius和shadow?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这让我很沮丧。我有一个UIView(称其为父)。该视图的最底部子视图是UIImageView(称为子),其框架占据了整个父范围。

This is stumping me. I have a UIView (call it "parent"). The bottommost subview of that view is a UIImageView (call it "child"), whose frame occupies the entirety of the "parent" bounds.

我想绕过角落在父视图上,并设置阴影。我照常在父级的 CALayer 上执行此操作:

I want to round the corners on the "parent" view, and set a drop shadow. I do this on the CALayer of "parent" as usual:

[[parent layer] setShadowOffset:CGSizeMake(5, 5)];
[[parent layer] setShadowRadius:6];
[[parent layer] setShadowOpacity:0.4];    
[[parent layer] setCornerRadius:6];

这可以正确显示阴影,但不会变角。

This shows the shadow correctly, but does not round the corners.

这里是踢腿者:


  1. 如果我删除子图像视图,或缩小它,使其不占据父视图的整个范围,我可以在父对象上正确获得圆角和阴影。

  2. 如果我离开子一个人,但是在父视图上设置 clipsToBounds,我正确地获得了角落。但是现在阴影消失了。

  3. 层上设置拐角半径似乎也无效。

  1. If I remove the "child" image view, or shrink it so it doesn't occupy the whole bounds of the "parent" view, I get the rounded corners and shadow correctly on the parent.
  2. If I leave the "child" alone but set "clipsToBounds" on the "parent" view, I get the corners correctly. But now the shadow's gone.
  3. Setting the corner radius on the layer of the child as well seems to have no effect.

子级图像视图似乎遮盖了父级视图上的圆角,因为它占据了整个矩形,并基于父级进行裁剪视图可以遮盖住角落,但也可以遮盖阴影。不知道为什么#3不起作用。

It seems like the "child" image view is just obscuring the rounded corners on the "parent" view since it takes up the whole rect, and clipping based on the parent view gets the corners but also masks off the shadow. Not sure why #3 doesn't work.

我想念什么?盯着这么长的时间,我是否一直在忽略一些明显的东西?

What am I missing? Have I been overlooking something obvious by staring at this too long?

谢谢。

(令人震惊的是,标记

推荐答案

您将需要两个嵌套视图,其中一个内部设置了圆角,剪切到边界,并且外部视图具有阴影(因此没有剪切)。在您的情况下,内部和外部视图可能是子视图和父视图,但是我想您没有为这些视图设置正确的裁剪值?

You will need two nested views, the inner one setting rounded corners and clipping to bound, and the outer view having the shadow (and therefore not clipping). In your case inner and outer view will probably be "child" and "parent", but I guess you didn't set the right clipping values for these views?

请参见为什么masksToBounds = YES可以防止CALayer阴影?

这篇关于无法在图像视图延伸到其边界的图层上设置cornerRadius和shadow?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆