简体   繁体   English

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

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

I am using tableview cell Reorder control in my application and it works well till iOS 14 but not working on iOS 15. In iOS 15 Reorder Control's color is not changed.我在我的应用程序中使用 tableview 单元格重新排序控件,它在 iOS 14 之前运行良好,但在 iOS 15 上不起作用。在 iOS 15 中,重新排序控件的颜色没有改变。

Following is code what I used.以下是我使用的代码。 So how can I change Reorder Control's color in iOS 15.那么如何在 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;
}
}
}
                

In iOS 15, I tried to customise Reorder control's icon but failed to do that.在 iOS 15 中,我尝试自定义重新排序控件的图标,但未能做到。 So I simply remove UIImageview object from Reorder control.所以我只是从重新排序控制中删除 UIImageview object 。 And create my own image view object programmatically and set image to that imageView.并以编程方式创建我自己的图像视图 object 并将图像设置为该 imageView。 And after that add that imageView as addSubview into Reorder control.然后将 imageView 作为 addSubview 添加到重新排序控件中。 And it solved my problem.它解决了我的问题。

Follow sharing code so it help to others who face same issue.关注共享代码,以便对面临相同问题的其他人有所帮助。

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

This is the objective code solution created based on the previous answer with some minor changes:这是基于先前答案创建的目标代码解决方案,并进行了一些小改动:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM