无法在按钮和UIView周围创建阴影

问题描述 投票:0回答:1

我在我的应用程序中添加了一个按钮并使用以下代码进行设置:

locationButton.layer.cornerRadius = 7
locationButton.clipsToBounds = true
locationButton.layer.shadowColor = UIColor.black.cgColor
locationButton.layer.shadowOffset = CGSize(width: 0, height: 3)
locationButton.layer.shadowRadius = 3
locationButton.layer.shadowOpacity = 1
locationButton.layer.masksToBounds = true

我用UIView做了同样的事情

profileView.layer.shadowColor = UIColor.black.cgColor
profileView.layer.shadowOffset = CGSize(width: 0, height: 5)
profileView.layer.shadowRadius = 10
profileView.layer.shadowOpacity = 0.5
profileView.layer.masksToBounds = true

该按钮显示时根本没有阴影,而视图的阴影不在边框周围,而是在其内部的元素周围。

ios swift button view shadow
1个回答
1
投票

这是工作代码

button.layer.masksToBounds = false
button.layer.shadowColor = UIColor.red.cgColor
button.layer.shadowOffset = CGSize(width: 5, height: 5)
button.layer.shadowOpacity = 1
button.layer.borderWidth = 1

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.