Ios 更改再订购控制';表视图单元格中的s颜色

Ios 更改再订购控制';表视图单元格中的s颜色,ios,swift,uitableview,user-interface,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

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

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

除非Swift中有什么变化或其他人设法找到答案,否则我对此进行了很好的研究,而且似乎没有办法不更改
UIReorderControl
文件就可以做到这一点,这可能会导致你的应用被拒绝


我的解决方案是实现“长点击和拖动”,在线上有教程。不幸的是,这并不理想,也不太容易实现。

这在我使用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;
        }
    }
}

从上面Thomas answer的启发中,我找到了一个适合我的解决方案

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

我使用一个扩展来查找reorder控件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中,我更新图像视图的颜色

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

为了防止有人回到过去,这里是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

在iOS13.x中,重新排序控件的颜色似乎取决于亮/暗模式(之前它设置为与背景对比,现在它看起来是固定的,取决于模式)。 因此,可以通过UITableViewCell的
overrideUserInterfaceStyle
切换重新排序控件的颜色

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

现在申请

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

   return cell;
}
给出以下结果:


如果整个应用程序的要求是白色,请尝试将全局色调更改为白色。这不是附件视图吗?也许有什么需要设置的,不是accessoryView的淡色或表视图的淡色。我也不希望应用程序中的每个单元格都是白色。理想情况下,此表格视图中的每个单元格都不是白色的。我认为解决您问题的一个好办法是使用两个图像(黑色图像和白色图像)作为辅助视图。它是您单元格中的自定义按钮吗?回答我一直在寻找那些实施独立于明暗模式的颜色方案的人(自从iOS 5以来,我的应用程序已经有了十几种深色方案),苹果的劣质实现一直存在问题。例如,复选标记附件符合tintColor;披露指示器不符合。我一直在使用自定义附件视图进行披露,但使用此解决方案,我将摆脱它。我还将此解决方案用于重订控制。我测试我的cell bg颜色亮度并选择UIInterfaceeStyleDark/Light适当,以诱使苹果适当地对披露/重新排序进行着色。效果很好。这在Swift中也适用于我。您只需将颜色设置调整为不可知,这是meh考虑到修复有多容易。这在Xamarin.ios中有效。只需更改为大写即可