ios - 如何在 UIView 顶部设置阴影?
问题描述
class AccountListViewController: UIViewController {
@IBOutlet weak var actionButtonView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
actionButtonView.layer.cornerRadius = 10
actionButtonView.layer.shadowColor = UIColor.gray.cgColor
actionButtonView.layer.shadowOffset = .zero
actionButtonView.layer.shadowOpacity = 0.6
actionButtonView.layer.shadowRadius = 10
}
我试过这个但没有得到结果
解决方案
您的代码很好,但您需要为阴影偏移指定负值而不是.zero
class AccountListViewController: UIViewController {
@IBOutlet weak var actionButtonView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
actionButtonView.layer.cornerRadius = 10
actionButtonView.layer.shadowColor = UIColor.gray.cgColor
actionButtonView.layer.shadowOffset = CGSize(width: 0.0, height : -5.0)
actionButtonView.layer.shadowOpacity = 0.6
actionButtonView.layer.shadowRadius = 10
}
}
为了更方便,您可以为此创建一个扩展。
请参考以下代码
extension UIView {
func addTopShadow(shadowColor : UIColor, shadowOpacity : Float,shadowRadius : Float,offset:CGSize){
self.layer.shadowColor = shadowColor.cgColor
selflayer.shadowOffset = offset
self.layer.shadowOpacity = shadowOpacity
self.layer.shadowRadius = shadowRadius
self.clipsToBounds = false
}
}
如何使用?
viewName.addTopShadow(shadowColor: UIColor.gray, shadowOpacity: 0.9, shadowRadius: 10, offset: CGSize(width: 0.0, height : -5.0))
推荐阅读
- python - 从 Python 中的字符串中删除列表中的模式
- sql - 使用 NULL 作为通配符的 SQL “重复数据删除”
- apache - Apache 2.4 太多重定向,www 到非 www 和 http 到 https
- excel - 如何定位从特定行开始的每第 n 行以更改字体颜色和样式?
- javascript - discord.js 列出具有一定作用的频道
- javascript - 变量不包含对“this”的引用
- multithreading - 在 Python 中从线程向主发出 g_signal
- html - 为什么我的元素比指定高度高 4px?
- stream - Google Nearby 连接 - 无法在 2 个设备之间传输大字节
- javascript - 对象中的扩展语法如何工作?