【问题标题】:UITableViewCell Reorder Control - set backgroundColorUITableViewCell 重新排序控件 - 设置 backgroundColor
【发布时间】:2022-03-31 16:04:51
【问题描述】:

我的表格视图中有一个自定义表格视图单元格,其中一部分背景为白色,另一部分为灰色。一切都像魅力一样 - 直到重新排序出现:

我的问题是重新排序控件全是灰色的,但我希望它部分是白色的,基本上它看起来像表格的一部分。我可以使用以下代码进入视图:

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

但是:在此处将视图的背景颜色设置为白色将如下所示:

我显然不想要 - 我想让灰色一直到右侧。 我尝试了视图的各种其他修改(例如,将框架的高度设置得更小,CGTransform 等)似乎没有任何影响!?

我真的很感激任何解决这个问题的提示! 谢谢!

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    这段代码应该在你的 UITableViewCell 中工作。它在其上激活editMode,查找,隐藏子视图并调整整个单元格上的重新排序控件的大小。关键是使用 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
    }
    

    【讨论】:

      【解决方案2】:

      1.将灰线放在节标题视图上;

      2.设置单元格的backgroundColor为白色;

      YourCell: UITableViewCell {
          override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
              super.init(style: style, reuseIdentifier: reuseIdentifier)
              self.backgroundColor = .white
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-07-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-16
        • 1970-01-01
        相关资源
        最近更新 更多