简体   繁体   English

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

[英]Drop a shadow to right and bottom of uiview

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. 这仍然在顶部留下了阴影。我应该怎么做才能获得底部的视图。

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: 我测试了这段代码,它的工作原理是:

在此处输入图片说明

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. masksToBounds也是imp的,因为它禁用了超出视图范围的子层的裁剪。 If you put it YES then you won't see shadow as it clips sublayer where else in NO it allow to extent layer. 如果将其设置为“是”,那么您将看不到阴影,因为它会将子图层剪切到其他位置,否则以“否”的方式扩展图层。

In Swift 3 , CGSizeMake no longer exists. Swift 3中CGSizeMake不再存在。 It has been changed to CGSize(width: 20, height: 10) . 它已更改为CGSize(width: 20, height: 10) So the shadowOffset can be set like this in Swift 3 : 因此可以在Swift 3中像这样设置shadowOffset

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

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

在此处输入图片说明

I think your shadow offset is incorrect. 我认为您的阴影偏移量不正确。 It should be { 10 , 10} like: 应该是{10,10}像:

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

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

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