swift 设置backgroundColor

zwghvu4y  于 11个月前  发布在  Swift
关注(0)|答案(3)|浏览(128)

我在表格视图中有一个自定义表格视图单元格,其中部分背景是白色,另一部分是灰色。工作起来就像一个魅力-直到重新排序显示:
x1c 0d1x的数据
我的问题是,重新排序控件都是灰色的,但我希望它是部分白色,基本上,它看起来像表的一部分。我可以使用以下代码到达视图:

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

字符串
但是:在这里将视图的背景色设置为白色,看起来像这样:



我显然不想这样--我想让灰色一直移到右边。我尝试了各种其他的视图修改(例如,将框架的高度设置得小一点,CGTransform等),但似乎没有任何影响!
我真的很感激任何提示来解决这个问题!谢谢!

yvgpqqbh

yvgpqqbh1#

这段代码应该在你的可编辑单元格中工作。它激活了它的editMode,找到,隐藏子视图,并调整整个单元格的Reorder控件。关键是使用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
}

字符串

vfwfrxfs

vfwfrxfs2#

1.将灰线放在节头视图上;
2.将单元格的backgroundColor设置为白色;

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

字符串

nimxete2

nimxete23#

最简单的方法是使用单元格的backgroundView。

class CustomCell : UITableViewCell {
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        self.setupAtInit()
    }
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.setupAtInit()
    }
    func setupAtInit() {
        self.backgroundVisew = UIView() //<- Here
        self.backgroundView?.backgroundColor = UIColor.white //The color you want to.
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        // You can layout backgroundView as you want.
        // Basically, the backgroundView bounds is the same as self.bounds.
        // self.backgroundView?.frame == self.bounds
    }
}

字符串

相关问题