ios - UITableViewCell 圆角对于某些单元格不可见
问题描述
我的表格使用圆角单元格作为部分。但是,在某些 iOS 设备上,右侧的圆角是不可见的。这可能不太可能与代码相关,更多的是在约束端。
下面的屏幕截图显示了圆角的工作位置(绿色框)和失败的位置(红色框)
我尝试了以下代码来添加似乎工作正常的圆角:
let path = UIBezierPath(roundedRect: cell.bounds,
byRoundingCorners:[.topRight, .topLeft], // example
cornerRadii: CGSize(width: 15, height: 15))
let maskLayer = CAShapeLayer()
maskLayer.path = path.cgPath
cell.layer.mask = maskLayer
我的单元格是这样初始化的,添加内容时我不调整它的大小:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: "mycell")
我有一种感觉,添加到单元格的内容会推动隐藏圆角的单元格宽度。有什么想法可能是错的吗?
解决方案
您需要继承 UITableViewCell 并覆盖该layoutSubview()
函数并设置那里的路径CAShapeLayer
。
final class MyTableViewCell: UITableViewCell {
private lazy var maskLayer = CAShapeLayer()
var corners: UIRectCorner = [] {
didSet {
setNeedsLayout()
updatePath(with: corners)
}
}
override func layoutSubviews() {
super.layoutSubviews()
updatePath(with: corners)
}
private func updatePath(with corners: UIRectCorner) {
let path = UIBezierPath(
roundedRect: bounds,
byRoundingCorners: corners,
cornerRadii: CGSize(width: 15, height: 15)
)
maskLayer.path = path.cgPath
layer.mask = maskLayer
}
}
然后通过角落cellForRowAt
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = MyTableViewCell(
style: UITableViewCell.CellStyle.default, reuseIdentifier: "mycell"
)
cell.corners = [.topRight, .topLeft]
// ...
return cell
}
推荐阅读
- reactjs - 开玩笑的 axios.create.mockImplementation 的 axios.create 返回 undefined
- ios - 如何在 SwiftUI 中使子视图阴影从父视图中溢出?
- kotlin - Kotlin gradle 对它的项目主页感到困惑
- wso2 - 尝试使用 Gmail 连接器发送电子邮件时发生 FileNotFoundException
- html - 你能找出错误吗?我正在尝试调试代码,但我看不到错误
- jquery - 为什么我的脚本隐藏元素会有延迟?
- r - 如何使用ggplot中的interaction()创建两个子组的图(几个图的组合)
- ios - SwiftUI Stack Offset 在旋转时发生变化导致崩溃
- flutter - 通过使用flutter http请求获取数据并在向下滚动屏幕时加载更多数据
- linux - 如何在 Rust 中获取打开的 std::fs::File 的文件名?