我在表格视图中有一个自定义表格视图单元格,其中部分背景为白色,另一部分为灰色.工作都像一个魅力 - 直到重新排序显示:
我的问题是重新排序控件都是灰色的,但我希望它部分是白色的,基本上它看起来像是表的一部分.我可以使用以下代码进入视图:
for view in cell.subviews {
if String(describing: view.self).contains("UITableViewCellReorderControl") {
view.backgroundColor = .white
}
}
Run Code Online (Sandbox Code Playgroud)
但是:在这里将视图的背景颜色设置为白色将如下所示:
我显然不想要 - 我希望灰色一直到右侧.我尝试了对视图的各种其他修改(例如设置框架的高度稍微小一些,CGTransform等)似乎什么都没有任何影响!?
我真的很感激任何提示来解决这个问题!谢谢!
这段代码应该在你的 UITableViewCell 中工作。它会激活编辑模式,查找、隐藏子视图并调整整个单元格上的重新排序控件的大小。关键是使用layoutSubviews()
weak var reorderControl: UIView?
override public func awakeFromNib() {
super.awakeFromNib()
setEditing(true, animated: false)
reorderControl = findTheReorderControl()
removeSubviews(from: reorderControl)
}
private func findTheReorderControl() -> UIView? {
return subviews.first { view -> Bool in
let className = String(describing: type(of:view))
return className == "UITableViewCellReorderControl"
}
}
private func removeSubviews(from reorderControl: UIView?) {
reorderControl?.subviews.forEach({$0.removeFromSuperview()})
}
override var showsReorderControl: Bool {
get {
return true // short-circuit to on
}
set {}
}
override func setEditing(_ editing: Bool, animated: Bool) {
if editing == false {
return // ignore any attempts to turn it off
}
super.setEditing(editing, animated: animated)
}
override func layoutSubviews() {
super.layoutSubviews()
reorderControl?.frame = bounds
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
527 次 |
| 最近记录: |