UITableViewCell Reorder Control - set backgroundColor

I have a custom table view cell in my table view, where part of the background is white and the other part is gray. Everything works like a charm - until reordering appears:

enter image description here

My problem is that all reordering controls are gray, but I want them to be partially white, mainly so that they look like part of the table. I can get to the view using this code:

        for view in cell.subviews {
            if String(describing: view.self).contains("UITableViewCellReorderControl") {
                view.backgroundColor = .white
            }
        }

BUT: setting the background color for the view here will look like this:

enter image description here

which I obviously don’t want - I would like the gray to go to the right side. I tried all kinds of other view modifications (for example, set a slightly lower frame height, CGTransform, etc.), It seems that nothing has any effect!?

, ! !

+5
1

UITableViewCell. editMode , , Reorder Control . 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
}
0

Source: https://habr.com/ru/post/1675214/


All Articles