In the image below, how do I change the color of the buttons on the right hand side of the view to white?edit: Ideally only want it to be white for certain cells and black for others Here's my code:
cell.backgroundColor = .appOrange
cell.contentLabel.textColor = .white
cell.numberLabel.textColor = .white
cell.tintColor = .white //this appears to do nothing, just something I tried
In iOS13.x seems like reorder control's color is dependent on the Light/Dark mode (earlier it was set on contrast with background, now it appears fixed depending on the mode).
Therefore there is a possibility to switch color of the reorder control by overrideUserInterfaceStyle
of the UITableViewCell.
Default - in the light mode of the phone I get practically invisible controls:
Now applying
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
SelectableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"selectableCell"];
...
cell.overrideUserInterfaceStyle = UIUserInterfaceStyleDark;
return cell;
}
gives following result:
I use an extension to find the reorder control 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
}
}
In willDisplay I update the tintColor of the image view.
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.reorderControlImageView?.tint(color: Color.text.uiColor)
}
Setting a tint color with a UIImageView extension
extension UIImageView {
func tint(color: UIColor) {
self.image = self.image?.withRenderingMode(.alwaysTemplate)
self.tintColor = color
}
}
This works for me in my UITableViewController using iOS11 and 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;
}
}
}
With inspiration from Thomas answer above, I found a solution that works for me.
Trying Thomas solution, I was faced with the problem, that it does not update the view for cells that are already displayed. Therefore I chose to overwrite the setEditing method on my custom cells instead.
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;
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With