iOS 15下如何修改UITableViewCell中Reorder Control重排控件的颜色
iOS 15 下 UITableViewCell 重排控件颜色修改失效解决方案
根因说明
iOS 15 重构了 UITableViewCellReorderControl 的内部视图层级:原有重排控件直接作为UITableViewCell的一级子视图,iOS 15 后被嵌套到了更深的视图层级下,同时系统会在willDisplay回调执行完成后重置重排控件内部UIImageView的属性,导致原有修改被覆盖。
兼容全系统版本的实现代码
private var customReorderImage: UIImage? = nil override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { // 递归遍历所有子视图查找重排控件,兼容不同iOS版本的视图层级差异 func findReorderControl(in view: UIView) -> UIView? { for subview in view.subviews { if NSStringFromClass(type(of: subview)) == "UITableViewCellReorderControl" { return subview } if let target = findReorderControl(in: subview) { return target } } return nil } guard let reorderControl = findReorderControl(in: cell), let imageView = reorderControl.subviews.first(where: { $0 is UIImageView }) as? UIImageView else { return } // 初始化模板渲染模式的重排图片 if customReorderImage == nil { customReorderImage = imageView.image?.withRenderingMode(.alwaysTemplate) } // 设置自定义颜色 imageView.image = customReorderImage imageView.tintColor = .red // iOS15+ 额外异步设置一次,避免系统后续布局逻辑覆盖自定义属性 if #available(iOS 15, *) { DispatchQueue.main.async { imageView.image = self.customReorderImage imageView.tintColor = .red } } }
更稳定的替代方案(不依赖私有视图层级)
如果不想因为后续系统版本迭代再次出现兼容问题,可以采用自定义视图的方案:
- 自行设计所需样式的重排图标
- 在UITableViewCell上添加自定义UIImageView,覆盖到系统重排控件的对应位置
- 禁用系统重排控件的交互,完全用自定义视图实现重排引导效果
内容的提问来源于stack exchange,提问作者Protocol
相关产品推荐
相关产品推荐

