ios 在 uiview 的右侧和底部放置一个阴影

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23149478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:58:30  来源:igfitidea点击:

Drop a shadow to right and bottom of uiview

ioscalayershadow

提问by Honey

I have to drop a shadow to the right and bottom of uiview.Im doing this in interface builder.But I see the shadow dropped to top of it.Tried differnt sizes.but couldn't get it.

我必须在 uiview 的右侧和底部放置一个阴影。我在界面构建器中这样做。但我看到阴影下降到它的顶部。尝试了不同的尺寸。但无法得到它。

layer.masksToBound=No
layer.shadowOpacity=0.15
layer.shadowRadius=2
layer.shadowOffSet={10,-10}   //Values being set in Interfacebuilder.

Still this drops shadow at top.What should I do to get at bottom of view.

这仍然会在顶部留下阴影。我应该怎么做才能到达视图底部。

回答by Nitin Gohel

Try the following code, it might help you

试试下面的代码,它可能对你有帮助

    myView.layer.shadowColor = [UIColor purpleColor].CGColor;
    myView.layer.shadowOffset = CGSizeMake(5, 5);
    myView.layer.shadowOpacity = 1;
    myView.layer.shadowRadius = 1.0;
    myView.layer.maskToBounds = NO;

I tested this code and it's working and output is:

我测试了这段代码,它工作正常,输出是:

enter image description here

在此处输入图片说明

回答by nikhil84

Hi I have used below code ,it will provide you with shadow you want.

嗨,我使用了下面的代码,它将为您提供您想要的阴影。

 UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:_viewShadow.bounds];
_viewShadow.layer.masksToBounds = NO;
_viewShadow.layer.shadowColor = [UIColor blackColor].CGColor;
_viewShadow.layer.shadowOffset = CGSizeMake(10.0f, 5.0f);  /*Change value of X n Y as per your need of shadow to appear to like right bottom or left bottom or so on*/
_viewShadow.layer.shadowOpacity = 0.5f;
_viewShadow.layer.shadowPath = shadowPath.CGPath;

Also masksToBounds is imp as it disables the clipping of sublayers that extend further than the view's bounds. If you put it YES then you won't see shadow as it clips sublayer where else in NO it allow to extent layer.

此外,masksToBounds 也是imp,因为它禁用了超出视图边界的子图层的裁剪。如果您将其设置为 YES,那么您将不会看到阴影,因为它会剪辑子层,而在 NO 中则允许扩展层。

回答by Sriharsha Guduguntla

In Swift 3, CGSizeMakeno longer exists. It has been changed to CGSize(width: 20, height: 10). So the shadowOffsetcan be set like this in Swift 3:

Swift 3 中CGSizeMake不再存在。它已更改为CGSize(width: 20, height: 10). 所以shadowOffset可以在Swift 3 中这样设置:

myView.layer.shadowOffset = CGSize(width: 20, height: 10)

回答by Frédéric Adda

I found out that these values give a nice result :

我发现这些值给出了一个很好的结果:

myView.layer.shadowColor = UIColor.black.cgColor
myView.layer.shadowOpacity = 0.25
myView.layer.shadowRadius = 3
myView.layer.shadowOffset = CGSize(width: 1, height: 1) // shadow on the bottom right

enter image description here

在此处输入图片说明

回答by Gaurav Singh

I think your shadow offset is incorrect. It should be { 10 , 10} like:

我认为您的阴影偏移不正确。它应该是 { 10 , 10} 像:

[layer setShadowOffset:CGSizeMake( 10 , 10 ) ];