iOS 在视图中添加/删除阴影
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16751163/
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
iOS add / remove shadow from a view
提问by Hw.Master
I do not understand how to remove a shadow that was added to a view.
I add to my view in initWithFrame
a shadow in this way:
我不明白如何删除添加到视图中的阴影。我以initWithFrame
这种方式在阴影中添加到我的视图中:
self.layer.borderWidth = 2;
self.layer.borderColor = [UIColor clearColor].CGColor;
self.backgroundColor = [UIColor greenColor];
[self.layer setCornerRadius:8.0f];
CALayer *layer = self.layer;
layer.shadowOffset = CGSizeMake(2, 2);
layer.shadowColor = [[UIColor blackColor] CGColor];
layer.cornerRadius = 8.0f;
layer.shadowRadius = 3.0f;
layer.shadowOpacity = 0.80f;
layer.shadowPath = [[UIBezierPath bezierPathWithRect:layer.bounds] CGPath];
After in the execution of the app I want to remove the shadow from this view. I've tried using:
在执行应用程序后,我想从此视图中删除阴影。我试过使用:
layer.hidden = YES;
or
或者
self.layer.hidden = YES;
but this hides the view completely, not just the added shadow.
但这完全隐藏了视图,而不仅仅是添加的阴影。
Is there a way to retrieve the added shadow from a view and then hide it? Thanks!
有没有办法从视图中检索添加的阴影然后隐藏它?谢谢!
回答by Guillaume Algis
I guess you could use the shadowOpacity
property of your CALayer
.
我想你可以使用shadowOpacity
你的CALayer
.
So this should work:
所以这应该有效:
self.layer.shadowOpacity = 0;
See the CALayer
's shadowOpacity
documentation page
And to show your shadow use:
并显示您的阴影使用:
self.layer.shadowOpacity = 1.0;
回答by lindon fox
Sorry, not sure the correctway, but have you tried changing the properties of the layer shadow
? For example, one of these;
抱歉,不确定正确的方法,但是您是否尝试过更改layer shadow
? 例如,其中之一;
layer.shadowOffset = CGSizeMake(0, 0);
layer.shadowColor = [[UIColor clearColor] CGColor];
layer.cornerRadius = 0.0f;
layer.shadowRadius = 0.0f;
layer.shadowOpacity = 0.00f;
回答by zeeshan
Swift 4.2
斯威夫特 4.2
I am using this in my code for labels and navigation bar.
我在我的标签和导航栏代码中使用它。
extension UIView {
func shadow(_ height: Int = 5) {
self.layer.masksToBounds = false
self.layer.shadowRadius = 4
self.layer.shadowOpacity = 1
self.layer.shadowColor = UIColor.gray.cgColor
self.layer.shadowOffset = CGSize(width: 0 , height: height)
}
func removeShadow() {
self.layer.shadowOffset = CGSize(width: 0 , height: 0)
self.layer.shadowColor = UIColor.clear.cgColor
self.layer.cornerRadius = 0.0
self.layer.shadowRadius = 0.0
self.layer.shadowOpacity = 0.0
}
}
回答by A'sa Dickens
the "layer" that you are trying to make hidden is the layer of the object that you are having a shadow to it's not a visible aspect.. only the objects with in the layer... it's rather confusing to conceptualize anyways, the only way to remove the shadow is to undo what you originally did, which was suggested above, there is no defined property that you can just toggle a bool and make the shadow go away
你试图隐藏的“层”是你有阴影的对象的层,它不是一个可见的方面......只有层中的对象......无论如何概念化是相当混乱的,唯一的删除阴影的方法是撤消您最初所做的,这是上面建议的,没有定义的属性,您可以只切换一个布尔值并使阴影消失
回答by Fernando Cardenas
Swift 5.+
斯威夫特 5.+
My solution was to add a shadowBackgroundView
, which has a removable shadowLayer
. In this way I could easyly remove the layer without resetting the shadow properties.
我的解决方案是添加一个shadowBackgroundView
,它有一个可移动的shadowLayer
. 通过这种方式,我可以轻松删除图层而无需重置阴影属性。
class ViewController: UIViewController {
private let shadowBackgroundView: UIView = {
let view = UIView(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
view.layer.masksToBounds = false
return view
}()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
view.addSubview(shadowBackgroundView)
// the view you want to add the shadow
let dummyView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
dummyView.backgroundColor = .red
shadowBackgroundView.addSubview(dummyView)
let addShadowButton = UIButton(frame: CGRect(x: 100, y: 300, width: 140, height: 50))
addShadowButton.backgroundColor = .blue
addShadowButton.setTitle("Add Shadow", for: .normal)
addShadowButton.addTarget(self, action: #selector(addShadow), for: .touchUpInside)
let removeShadowButton = UIButton(frame: CGRect(x: 100, y: 450, width: 140, height: 50))
removeShadowButton.backgroundColor = .blue
removeShadowButton.setTitle("Remove Shadow", for: .normal)
removeShadowButton.addTarget(self, action: #selector(removeShadow), for: .touchUpInside)
view.addSubview(addShadowButton)
view.addSubview(removeShadowButton)
}
@objc
func addShadow() {
let shadowLayer = CALayer()
shadowLayer.name = "ShadowLayer"
shadowLayer.shadowColor = UIColor.black.cgColor
shadowLayer.shadowOpacity = 1
shadowLayer.shadowOffset = .zero
shadowLayer.shadowRadius = 10
shadowLayer.shadowPath = UIBezierPath(rect: shadowBackgroundView.bounds).cgPath
// Otherwise the shadow will appear above the dummyView
shadowLayer.zPosition = -1
shadowBackgroundView.layer.addSublayer(shadowLayer)
}
@objc
func removeShadow() {
// Alternatively, you could also create the shadowLayer as a property, so you could call shadowLayer.removeFromSuperLayer()
shadowBackgroundView.layer.sublayers?.first { ##代码##.name == "ShadowLayer" }?.removeFromSuperlayer()
}
}
As a Note, for the UITableViewCell
, you wouldnt need to add a shadowBackgroundView
, but you could add the shadowLayer
directly to cell.view.layer
, which serves as the backgroundView for the cell.contentView
.
请注意,对于UITableViewCell
,您不需要添加shadowBackgroundView
,但您可以shadowLayer
直接添加cell.view.layer
,作为 的背景视图cell.contentView
。