在表格视图单元格中更改重新排序控件的颜色

Kyl*_*ers 5 user-interface uitableview ios swift

在下图中,如何将视图右侧的按钮的颜色更改为白色?编辑:理想情况下,只希望某些单元格为白色,其他单元格为黑色,这是我的代码:

cell.backgroundColor = .appOrange
cell.contentLabel.textColor = .white
cell.numberLabel.textColor = .white
cell.tintColor = .white //this appears to do nothing, just something I tried
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

Nic*_*tin 14

在 iOS13.x 中,重新排序控件的颜色似乎取决于 Light/Dark 模式(之前它被设置为与背景形成对比,现在它看起来是固定的,取决于模式)。因此,有可能通过overrideUserInterfaceStyleUITableViewCell来切换重新排序控件的颜色。

默认 - 在手机的灯光模式下,我得到了几乎不可见的控件:

默认 - 在手机的灯光模式下,我得到了几乎不可见的控件

现在申请

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ...
    SelectableCell *cell = [tableView     dequeueReusableCellWithIdentifier:@"selectableCell"];
    ...
    cell.overrideUserInterfaceStyle = UIUserInterfaceStyleDark;

   return cell;
}
Run Code Online (Sandbox Code Playgroud)

给出以下结果:

覆盖 - UI 样式


har*_*lix 13

我使用一个扩展来查找重新排序控件 imageView。

extension UITableViewCell {

    var reorderControlImageView: UIImageView? {
        let reorderControl = self.subviews.first { view -> Bool in
            view.classForCoder.description() == "UITableViewCellReorderControl"
        }
        return reorderControl?.subviews.first { view -> Bool in
            view is UIImageView
        } as? UIImageView
    }
}
Run Code Online (Sandbox Code Playgroud)

在 willDisplay 中,我更新了图像视图的 tintColor。

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    cell.reorderControlImageView?.tint(color: Color.text.uiColor)
}
Run Code Online (Sandbox Code Playgroud)

使用 UIImageView 扩展设置色调颜色

extension UIImageView {

    func tint(color: UIColor) {
        self.image = self.image?.withRenderingMode(.alwaysTemplate)
        self.tintColor = color
    }
}
Run Code Online (Sandbox Code Playgroud)


Tho*_*mas 12

这在我的 UITableViewController 中使用 iOS11 和 Swift 4 对我有用:

private var myReorderImage : UIImage? = nil;

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    for subViewA in cell.subviews {
        if (subViewA.classForCoder.description() == "UITableViewCellReorderControl") {
            for subViewB in subViewA.subviews {
                if (subViewB.isKind(of: UIImageView.classForCoder())) {
                    let imageView = subViewB as! UIImageView;
                    if (myReorderImage == nil) {
                        let myImage = imageView.image;
                        myReorderImage = myImage?.withRenderingMode(UIImageRenderingMode.alwaysTemplate);
                    }
                    imageView.image = myReorderImage;
                    imageView.tintColor = UIColor.red;
                    break;
                }
            }
            break;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Mik*_*num 9

受上述 Thomas 回答的启发,我找到了一个适合我的解决方案。

尝试 Thomas 解决方案时,我遇到了问题,即它不会更新已经显示的单元格的视图。因此,我选择覆盖自定义单元格上的 setEditing 方法。

private var myReorderImage: UIImage? = nil

override func setEditing(_ editing: Bool, animated: Bool) {
    super.setEditing(editing, animated: animated)

    for subViewA in self.subviews {
        if (subViewA.classForCoder.description() == "UITableViewCellReorderControl") {
            for subViewB in subViewA.subviews {
                if (subViewB.isKind(of: UIImageView.classForCoder())) {
                    let imageView = subViewB as! UIImageView;
                    if (self.myReorderImage == nil) {
                        let myImage = imageView.image;
                        myReorderImage = myImage?.withRenderingMode(.alwaysTemplate);
                    }
                    imageView.image = self.myReorderImage;
                    imageView.tintColor = .red;
                    break;
                }
            }
            break;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Gom*_*ius 5

以防万一有人回到过去,这是 mikkel-cortnum 答案的 Objective-C 版本:

@implementation TintedReorderedCell

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];

    for (UIView *subViewA in self.subviews) {
        if ([NSStringFromClass(subViewA.class) isEqualToString:@"UITableViewCellReorderControl"]) {
            for (UIView *subViewB in subViewA.subviews) {
                if ([subViewB isKindOfClass:UIImageView.class]) {
                    UIImageView *imageView = ((UIImageView *)subViewB);
                    imageView.image = [imageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
                    imageView.tintColor = [UIColor redColor];
                    break;
                }
            }
            break;
        }
    }
}

@end
Run Code Online (Sandbox Code Playgroud)