[英]Change Reorder Control's color in table view cell
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).在 iOS13.x 中,重新排序控件的颜色似乎取决于 Light/Dark 模式(之前它设置为与背景形成对比,现在它看起来是固定的,取决于模式)。 Therefore there is a possibility to switch color of the reorder control by
overrideUserInterfaceStyle
of the UITableViewCell.因此,可以通过 UITableViewCell 的
overrideUserInterfaceStyle
来切换重新排序控件的颜色。
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.我使用一个扩展来查找重新排序控件 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.在 willDisplay 中,我更新了图像视图的 tintColor。
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使用 UIImageView 扩展设置色调颜色
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:这在我的 UITableViewController 中使用 iOS11 和 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.受上述 Thomas 回答的启发,我找到了一个适合我的解决方案。
Trying Thomas solution, I was faced with the problem, that it does not update the view for cells that are already displayed.尝试 Thomas 解决方案时,我遇到了问题,即它不会更新已经显示的单元格的视图。 Therefore I chose to overwrite the setEditing method on my custom cells instead.
因此,我选择覆盖自定义单元格上的 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;
}
}
}
And just in case if anyone is going back in time, here's Objective-C version of mikkel-cortnum's answer:以防万一如果有人回到过去,这里是 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
Unless something changes in Swift or someone else manages to dig up an answer, I researched this pretty well, and it looks like there isn't a way to do it without changing the UIReorderControl
file, which will probably get your app rejected.除非 Swift 中的某些变化或其他人设法找到答案,否则我对此进行了很好的研究,并且看起来没有办法在不更改
UIReorderControl
文件的情况下做到这UIReorderControl
,这可能会导致您的应用程序被拒绝。
My solution is to implement the "long tap and drag," which there are tutorials on online.我的解决方案是实现“长按拖动”,网上有教程。 It's not ideal and not incredibly easy to implement unfortunately.
不幸的是,这并不理想,也不是非常容易实施。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.