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

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

问题描述

这让我很难过.我有一个 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. child的图层上设置圆角半径好像也没有效果.
  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?

谢谢.

(令人震惊的是,roundedcorners-dropshadow"标签已经存在.太棒了.)

(Shockingly, the tag "roundedcorners-dropshadow" already exists. Awesome.)

推荐答案

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

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和阴影?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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