我有一个问题。
我想像这样制作关于角落的 View ,但我不知道该怎么做:
i want to make this corner view
我也关注了这个 Stackoverflow 问题 question url .
extension UIView {
func roundCorners(corners:UIRectCorner, radius: CGFloat) {
let mask = CAShapeLayer()
mask.path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius)).cgPath
layer.mask = mask
}
所以我在layoutsubview中使用corner函数
//var labelContent:UILabel
override func layoutSubviews() {
super.layoutSubviews()
labelContent.roundCorners(corners: [.bottomLeft,.topLeft,.topRight], radius: 10)
}
它可以工作,但是当我向上滚动时,单元格 View 消失了。
所以,我使用第二个功能。
但它也不起作用
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let cell: ChatContentTableViewCell = ChatContentTableViewCell(style: .default, reuseIdentifier: nil)
let maskPathTop = UIBezierPath(roundedRect: cell.bounds, byRoundingCorners: [.bottomRight], cornerRadii: CGSize(width: 0, height: 0))
let shapeLayerTop = CAShapeLayer()
shapeLayerTop.frame = cell.labelContent.bounds
shapeLayerTop.path = maskPathTop.cgPath
}
更新:我听从刺猬先生的建议:
当我向上滚动时,单元格 View 消失。
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
guard let cell = cell as? ChatContentTableViewCell else { return }
let maskPathTop = UIBezierPath(roundedRect: cell.contentView.bounds, byRoundingCorners: [.bottomRight], cornerRadii: CGSize(width: 0, height: 0))
let shapeLayerTop = CAShapeLayer()
shapeLayerTop.frame = cell.contentView.bounds
shapeLayerTop.path = maskPathTop.cgPath
}
最佳答案
第二种解决方案不起作用,因为您正在实例化单元格的新实例,而不是使用回调提供的实例。
将第一行改为:
guard let cell = cell as? ChatContentTableViewCell else { return }
然后尝试。第二件事——你确定你需要更新标签的角而不是包含它的内容 View 吗?
关于ios - 仅在 tableview 单元格中制作左下角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43665339/