我正在尝试为UIView
的背景(backgroundRect)添加渐变到UITableViewCell
。我期望渐变将在与backgroundRect相同的Z位置绘制,但是在设备上构建时,它会遮盖标签上方和其他视图(在顶部)。但是,令人困惑的是,当我使用ViewDebugger时,它显示我的视图好像它们应该在渐变层后面出现一样?
class NewWorkoutTableViewCell: UITableViewCell {
@IBOutlet weak var backgroundRect: UIView!
@IBOutlet weak var dayOfTheWeekLabel: UILabel!
@IBOutlet weak var sessionTypeLabel: UILabel!
@IBOutlet weak var dateLabel: UILabel!
@IBOutlet weak var sessionTypeImage: UIImageView!
@IBOutlet weak var scoreLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
backgroundRect.layer.cornerRadius = 8.0
backgroundRect.layer.shadowColor = UIColor.black.cgColor
backgroundRect.layer.shadowOpacity = 0.5
backgroundRect.layer.shadowOffset = CGSize(width: 5, height: 5)
backgroundRect.layer.shadowRadius = 5
// this is making a CoreAnimation gradient layer
let gradient = CAGradientLayer() // Line 1
// this is setting the dimensions of the gradient to the
// same as the view that will contain it
gradient.frame = backgroundRect.bounds // Line 2
//gradient.locations = [0.0, 0.35]
gradient.startPoint = CGPoint(x: 0.0, y: 0.5)
gradient.endPoint = CGPoint(x: 1.0, y: 0.5)
let iPhoneForegroundColor = UIColor(red:0.22, green:0.26, blue:0.40, alpha:1.0)
// this is setting the gradient from and to colors
gradient.colors = [UIColor.white.cgColor,iPhoneForegroundColor.cgColor] // Line 3
backgroundRect.layer.addSublayer(gradient) // Line 4
}
}
答案 0 :(得分:2)
只需使用
backgroundRect.layer.insertSublayer(gradient, at: 0)
代替使用
backgroundRect.layer.addSublayer(gradient)
答案 1 :(得分:0)
调试用户界面,看看添加的图层是否覆盖了子视图...我认为您的图层覆盖了子视图。