在 UITableView Cell 中放置阴影在 Swift 中不起作用
Dropping shadow inside UITableView Cell doesn't work in Swift
我的项目没有使用故事板,因为它使用的是 CleanSwift 架构。
每个视图都是以编程方式构建的。这是我的 table 单元格 class.
class LiveScoreCell: UITableViewCell {
var data: LiveScores.Data? {
didSet {
leagueLogo.setImage(data?.leagueLogo)
}
}
var container: UIView = {
let view = UIView()
view.backgroundColor = .white
view.layer.cornerRadius = 10
view.layer.shadowColor = UIColor.black.cgColor
view.layer.shadowOffset = .zero
view.layer.shadowOpacity = 0.6
view.layer.shadowRadius = 10
view.layer.shadowPath = UIBezierPath(rect: view.bounds).cgPath
view.layer.borderWidth = 1
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
var leagueLogo: UIImageView = {
let imgView = UIImageView()
imgView.translatesAutoresizingMaskIntoConstraints = false
return imgView
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
selectionStyle = .none
setupViews()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
selectionStyle = .none
setupViews()
}
override func prepareForReuse() {
super.prepareForReuse()
}
func setupViews() {
addSubview(container)
container.topAnchor.constraint(equalTo: self.topAnchor, constant: 8).isActive = true
container.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -8).isActive = true
container.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 8).isActive = true
container.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -8).isActive = true
addSubview(leagueLogo)
leagueLogo.topAnchor.constraint(equalTo: container.topAnchor, constant: 8).isActive = true
leagueLogo.leadingAnchor.constraint(equalTo: container.leadingAnchor, constant: 8).isActive = true
leagueLogo.widthAnchor.constraint(equalToConstant: 26).isActive = true
leagueLogo.heightAnchor.constraint(equalToConstant: 26).isActive = true
}
}
结果是..
只有边界涉及..知道为什么会这样吗?
截至本行
view.layer.shadowPath = UIBezierPath(rect: view.bounds).cgPath
当边界为零时运行,所以让它在里面
override func layoutSubviews() {
super.layoutSubviews()
container.layer.shadowPath = UIBezierPath(rect: container.bounds).cgPath
}
我的项目没有使用故事板,因为它使用的是 CleanSwift 架构。 每个视图都是以编程方式构建的。这是我的 table 单元格 class.
class LiveScoreCell: UITableViewCell {
var data: LiveScores.Data? {
didSet {
leagueLogo.setImage(data?.leagueLogo)
}
}
var container: UIView = {
let view = UIView()
view.backgroundColor = .white
view.layer.cornerRadius = 10
view.layer.shadowColor = UIColor.black.cgColor
view.layer.shadowOffset = .zero
view.layer.shadowOpacity = 0.6
view.layer.shadowRadius = 10
view.layer.shadowPath = UIBezierPath(rect: view.bounds).cgPath
view.layer.borderWidth = 1
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
var leagueLogo: UIImageView = {
let imgView = UIImageView()
imgView.translatesAutoresizingMaskIntoConstraints = false
return imgView
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
selectionStyle = .none
setupViews()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
selectionStyle = .none
setupViews()
}
override func prepareForReuse() {
super.prepareForReuse()
}
func setupViews() {
addSubview(container)
container.topAnchor.constraint(equalTo: self.topAnchor, constant: 8).isActive = true
container.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -8).isActive = true
container.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 8).isActive = true
container.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -8).isActive = true
addSubview(leagueLogo)
leagueLogo.topAnchor.constraint(equalTo: container.topAnchor, constant: 8).isActive = true
leagueLogo.leadingAnchor.constraint(equalTo: container.leadingAnchor, constant: 8).isActive = true
leagueLogo.widthAnchor.constraint(equalToConstant: 26).isActive = true
leagueLogo.heightAnchor.constraint(equalToConstant: 26).isActive = true
}
}
结果是..
只有边界涉及..知道为什么会这样吗?
截至本行
view.layer.shadowPath = UIBezierPath(rect: view.bounds).cgPath
当边界为零时运行,所以让它在里面
override func layoutSubviews() {
super.layoutSubviews()
container.layer.shadowPath = UIBezierPath(rect: container.bounds).cgPath
}