遇到問題
設計為了美觀,Cell上有一個小的三角形,本來就是想著,直接在cell上畫圖就完事了,但畫完以后,一直不顯示。使用UIBezierPath和UIGraphicsGetCurrentContext畫都不顯示,這就讓人有點頭禿了兄弟
探索
因為drawRect是在繼承于UIView的UITableViewCell層面進行繪制的,但TableViewCell有一個內(nèi)容視圖contentView,這里就是因為contentView.alpha不是0導致遮擋住了繪制在cell上的東西。
解決
我的解決方式,就是采用上面鏈接里面的那種方式,自定義視圖覆蓋在contentView上,在自定義視圖上面進行繪制。
class CustomView : UIView {
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = .white
}
override func draw(_ rect: CGRect) {
super.draw(rect)
guard let context = UIGraphicsGetCurrentContext() else {
return
}
context.setLineWidth(1)
context.setAlpha(1)
context.move(to: CGPoint(x: 32, y: 5))
context.addLine(to: CGPoint(x: 29, y : 10))
context.addLine(to: CGPoint(x: 35, y : 10))
context.closePath()
context.setFillColor(UIColor.hexStringToColor("f5f5f5").cgColor)
context.fillPath()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
}