ios 如何设置 UITableViewCell 的辅助颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4282385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I set a UITableViewCell's accessory color?
提问by iand675
I'd like to set the accessory view icon to be white, similarly to how icons are colored white automatically in a black TabBar, but it's not immediately obvious to me how to do this. Does anyone have experience with this?
我想将附件视图图标设置为白色,类似于图标在黑色 TabBar 中自动着色为白色的方式,但对我来说如何做到这一点并不是很明显。有任何人对此有经验吗?
采纳答案by Matt
Your best bet is to make an image that you want to put in there and then setting the accessory view to be that image. Here's Apple's Official Documentation for UITableViewCells.
最好的办法是制作一张你想要放入其中的图像,然后将附件视图设置为该图像。这是Apple 的 UITableViewCells 官方文档。
回答by HymanyJohnson
Actually the correct answer (despite it being so late) is to set your cell tintColor property to a white UIColor
object, like so:
实际上正确的答案(尽管这么晚了)是将您的单元格 tintColor 属性设置为白色UIColor
对象,如下所示:
UITableViewCell *cell = [UITableViewCell new]; // or other instantiation...
[cell setTintColor:[UIColor whiteColor]];
Subclassing the accessory view entirely is also possible, but the question is asking to make the cell accessory button white, so this should be the correct answer (at least for iOS7).
完全子类化附件视图也是可能的,但问题是要求将单元格附件按钮设为白色,所以这应该是正确的答案(至少对于 iOS7)。
Note: Corrected property name, also note that it doesn't actually work for the indicator accessory type
注意:更正了属性名称,还要注意它实际上不适用于指示器附件类型
回答by Akhil K C
Setting tint color for table view solves it for me.
为表格视图设置色调颜色为我解决了这个问题。
回答by Sandeep Jangir
UIImageView *checkmark = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"coloredCheckmark.png"]];
cell.accessoryView = checkmark;`
Enjoy
享受
回答by William Hu
Swift 3.0
斯威夫特 3.0
Two ways:
方法有两种:
- Use your own
UIImageView
asaccessoryView
of the tableview cell. If you are using the system accessory type, like
Disclosure Indicator
the arrow icon (>), you can change theUIImageView
tintColor
to achieve it. Like below:cell?.subviews.forEach({ if let btn =
as? UIButton { btn.subviews.forEach({ if let imageView =cell?.subviews.forEach({ if let btn =
as? UIButton { btn.subviews.forEach({ if let imageView =func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { guard let cell = tableView.dequeueReusableCell(withIdentifier: "MyCustomCellIdentifier", for: indexPath) as? MyCustomCell else { return UITableViewCell() } cell.tintColor = UIColor.white //Important cell.accessoryType = (someCondition) ? .checkmark : .none return cell }
as? UIImageView { let image = imageView.image?.withRenderingMode(.alwaysTemplate) imageView.image = image imageView.tintColor = UIColor.red } }) } })extension UITableViewCell { func addDisclosureIndicator(){ let button = UIButton(frame: CGRect(x: 0, y: 0, width: 15, height: 15)) /// 15 look great for me in iPhone and iPad button.setImage( UIImage(named: "rightArrow"), for: .normal) ///You can download a chevron of your choice online button.tintColor = UIColor(yourPreferredColor) self.accessoryView = button } }
as? UIImageView { let image = imageView.image?.withRenderingMode(.alwaysTemplate) imageView.image = image imageView.tintColor = UIColor.red } }) } })override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) cell.addDisclosureIndicator() return cell }
- 使用你自己
UIImageView
的accessoryView
了的tableview细胞。 如果您使用的是系统附件类型,如
Disclosure Indicator
箭头图标 (>),您可以更改UIImageView
tintColor
来实现它。像下面这样:UIImageView *trash_icon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ic_delete_record.png"]]; UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button.frame = CGRectMake(280, 140, 30, 30); //Here is where you change the color of the button. button.tintColor = UIColor.whiteColor; [button setImage: trash_icon.image forState:UIControlStateNormal]; cell.accessoryView = button;
回答by Hemang
override func awakeFromNib() {
super.awakeFromNib()
tintColor = UIColor.red
}
回答by Kiattisak Anoochitarom
The easiest way I would do is "create your accessory from image yourself" and change color it with template color options or just use an image that has color you want.
我会做的最简单的方法是“自己从图像创建您的配件”并使用模板颜色选项更改颜色或仅使用具有您想要的颜色的图像。
回答by Atka
A solution combining @BharathRao,@Hemang and @Kiattisak Anoochitarom ideas
结合@BharathRao、@Hemang 和@Kiattisak Anoochitarom 想法的解决方案
It seems that this a problem specially in iOS 13+ (changing tintColor
will make no difference) with more than 2000 views on this page on Apple Developer Forums
(https://forums.developer.apple.com/thread/121472)
tintColor
在 Apple 开发者论坛 ( https://forums.developer.apple.com/thread/121472) 的此页面上,该页面的浏览次数超过 2000 次,这似乎是 iOS 13+ 中的一个问题(更改不会有任何区别)
My solution using ideas from @BharathRao and @Hemang is to add an extension to UITableViewCell
named eg:addDisclosureIndicator
我使用来自@BharathRao 和@Hemang 的想法的解决方案是向UITableViewCell
名为 eg:addDisclosureIndicator添加一个扩展
Using an extension will make things more uniform in the app specially if there are multiple tables and storyboards.
使用扩展将使应用程序中的事情更加统一,特别是如果有多个表格和故事板。
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellModel = addressCellModels[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier:cellModel.cellIdentifier, for: indexPath) as! AddressSelectionCell
cell.tintColor = UIColor.red
return cell
}
Call
称呼
##代码##Hope this is helpful.
希望这是有帮助的。
回答by BharathRao
If you use custom image / button for your accessory view, you need to change the tint color of the button something like
如果您为附件视图使用自定义图像/按钮,则需要更改按钮的色调颜色,例如
##代码##回答by Ilker Baltaci
In your custom cell class
在您的自定义单元格类中
##代码##or
或者
##代码##