更改 table 视图单元格中 iOS 15 的重新排序控件的颜色

Change Reorder Control's color in table view cell for iOS 15

我在我的应用程序中使用 tableview cell Reorder 控件,它在 iOS 14 之前运行良好,但在 iOS 15 上不工作。在 iOS 15 中,Reorder 控件的颜色没有改变.

以下是我使用的代码。那么如何在 iOS 15.

中更改 Reorder Control 的颜色
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;
}
}
}
                

在iOS15 中,我尝试自定义Reorder 控件的图标但没有成功。所以我只是从 Reorder 控件中删除 UIImageview 对象。并以编程方式创建我自己的图像视图对象并将图像设置为该图像视图。然后将该 imageView 作为 addSubview 添加到 Reorder 控件中。它解决了我的问题。

关注共享代码,以帮助遇到同样问题的其他人。

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())) {
subViewB.removeFromSuperview()
let imageView = UIImageView()
 if (self.myReorderImage == nil) {
 let myImage = imageView.image
myReorderImage = myImage?.withRenderingMode(.alwaysTemplate)
}
var frame = imageView.frame
frame.origin.x = 3
frame.origin.y = 14
frame.size = CGSize(width: 21, height: 9)
self.myReorderImage = UIImage(named:"YourImage") // set your image 
imageView.frame = frame
imageView.image = self.myReorderImage
 subViewA.addSubview(imageView) // add imageView to reorder control
break;
 }
 }
break;
}
}
}

这是objective代码解决方案,基于之前的答案创建,并做了一些小改动:

// Change cell reorder control tint color to white color (while on editing mode)
for (UIView *subViewA in self.subviews)
{
    // Check if cell subview corresponds to cell rerorder control
    if ([NSStringFromClass (subViewA.class) isEqualToString: @"UITableViewCellReorderControl"])
    {
        for (UIView *subViewB in subViewA.subviews)
        {
            // Check if cell reorder control subview corresponds to image view
            if ([subViewB isKindOfClass: [UIImageView class]])
            {
                // Set reorderImage variable with current image view's image and rendering mode "always"
                self.reorderImage = [[(UIImageView *) subViewB image] imageWithRenderingMode: UIImageRenderingModeAlwaysTemplate];
                
                // Set new image view with current image view frame, reorder image variable, and white tint color
                UIImageView *imageView = [[UIImageView alloc] init];
                imageView.frame = subViewB.frame;
                imageView.image = self.reorderImage;
                imageView.tintColor = [UIColor whiteColor];
                
                // Remove old image view from cell reorder control view hierarchy
                [subViewB removeFromSuperview];
                
                // Add new image view to cell reorder control view hierarchy
                [subViewA addSubview: imageView];

                break;
            }
        }
    }
}