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

时间:2017-08-30 18:14:59

标签: ios swift uitableview user-interface

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

cell.backgroundColor = .appOrange
cell.contentLabel.textColor = .white
cell.numberLabel.textColor = .white
cell.tintColor = .white //this appears to do nothing, just something I tried

enter image description here

6 个答案:

答案 0 :(得分:9)

在iOS13.x中,似乎重新排序控件的颜色取决于明/暗模式(之前它是与背景形成对比的,现在根据模式而显得固定)。 因此,可以通过UITableViewCell的overrideUserInterfaceStyle切换重新排序控件的颜色。

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

Default - in the light mode of the phone I get practically invisible controls

现在申请

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

   return cell;
}

给出以下结果:

Overriding - UI style

答案 1 :(得分:2)

以防万一有人时光倒流,这是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

答案 2 :(得分:1)

除非Swift中的某些内容发生变化或其他人设法找到答案,否则我对此进行了很好的研究,看起来似乎没有办法在不更改UIReorderControl文件的情况下进行此操作可能会拒绝您的应用。

我的解决方案是实现"长按和拖动,"其中有在线教程。不幸的是,它并不理想,也不容易实现。

答案 3 :(得分:1)

这适用于我使用iOS11和Swift 4的UITableViewController:

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;
        }
    }
}

答案 4 :(得分:1)

从上面的托马斯回答中得到启发,我找到了适合我的解决方案。

尝试使用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;
        }
    }
}

答案 5 :(得分:0)

我使用扩展程序来查找重新排序控件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
    }
}

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

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

设置带有UIImageView扩展名的色彩

extension UIImageView {

    func tint(color: UIColor) {
        self.image = self.image?.withRenderingMode(.alwaysTemplate)
        self.tintColor = color
    }
}
相关问题