【问题标题】:Corner Radius of UITableViewCell Being Reset when Editing编辑时 UITableViewCell 的角半径被重置
【发布时间】:2019-11-25 03:34:41
【问题描述】:

我有一个 UITableView,它有一个 UITableViewCells 列表,其中包含自定义角半径。滑动编辑时,e.i.删除/插入,圆角半径重置为0。

我尝试在编辑时设置圆角半径:

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
    guard let cell = tableView.cellForRow(at: indexPath) as? HabitTableViewCell else { return }
    cell.layer.cornerRadius = 13
    if editingStyle == .delete {
        controller.delete(habit: frc.object(at: indexPath))
    }
}

我还尝试以类似的方式实现 willBeginEditingRowAt 以尝试使角半径保持不变。我尝试的最后一件事是在 trailingSwipeActionsConfigurationForRowAt 中创建自定义 UIContextualAction,但它并没有改变任何东西。

大约四年前有一个similar question asked,但从未找到最终的解决方案。感谢您提供任何帮助或见解!

【问题讨论】:

  • 你为什么不用'layoutSubviews'功能在'UITableViewCell'内制作'cornerRadius'?

标签: ios swift xcode uitableview cornerradius


【解决方案1】:

您需要将圆角半径和边框设置为contentView

cell.contentView.layer.cornerRadius = 13

将此代码添加到UITableViewCell 类的awakeFromNiblayoutSubviews 方法中。

override func awakeFromNib() {
    super.awakeFromNib()
    self.contentView.layer.cornerRadius = 13

    // Your border code here (set border to contentView)
    self.contentView.layer.borderColor = UIColor.blue.cgColor
    self.contentView.layer.borderWidth = 3
}

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-24
    • 1970-01-01
    • 1970-01-01
    • 2020-01-18
    • 2021-08-12
    • 2016-01-07
    • 1970-01-01
    相关资源
    最近更新 更多