我想给一个UIStackView添加阴影。我的stackview在一个原型单元格中,其中包含一些标签和按钮。 首先,在绑定到我的自定义单元格的类中,我使用名称UIViewBank创建了一个stackview的引用:
class CustChatBankCell: UITableViewCell {
@IBOutlet weak var UIViewBank: UIStackView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
在我的控制器中,我正在使用该扩展:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:CustChatBankCell = self.tbChat.dequeueReusableCell(withIdentifier: "CustomBankCell") as! CustChatBankCell
cell.UIViewBank.dropShadow()
return cell
}
}
扩展代码:
extension UIStackView {
func dropShadow(scale: Bool = true) {
self.layer.masksToBounds = false
self.layer.shadowColor = UIColor.black.cgColor
self.layer.shadowOpacity = 0.5
self.layer.shadowOffset = CGSize(width: -1, height: 501)
self.layer.shadowRadius = 1
self.layer.shadowPath = UIBezierPath(rect: self.bounds).cgPath
self.layer.shouldRasterize = true
self.layer.rasterizationScale = scale ? UIScreen.main.scale : 1
}
}
但阴影没有出现。