ios 更改附件类型 Swift 的颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29451482/
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
Change color of accessoryType Swift
提问by horst
I'd like to change the color of my cell accessoryType from blue to white. The textColor is already set to white. Does someone of you guys know how to do this?
我想将我的单元格附件类型的颜色从蓝色更改为白色。textColor 已经设置为白色。你们中有人知道如何做到这一点吗?
My Code:
我的代码:
cell!.accessoryType = UITableViewCellAccessoryType.Checkmark
cell!.accessoryType = UITableViewCellAccessoryType.Checkmark
回答by OscarVGG
You can set your UITableViewCell tintColor property to the desired color:
您可以将 UITableViewCell tintColor 属性设置为所需的颜色:
[cell setTintColor:[UIColor whiteColor]];
回答by Dasoga
Swift:
迅速:
cell.tintColor = UIColor.whiteColor()
Swift 3.0
斯威夫特 3.0
cell.tintColor = UIColor.white
回答by Alessandro Ornano
Swift 3.1:
斯威夫特 3.1:
cell.backgroundColor = UIColor.yellow // the accessoryType background
cell.tintColor = UIColor.black // the accessoryType tint color.
回答by Tung Fam
Doesn't work for .disclosureIndicator
?
不工作.disclosureIndicator
?
If someone is here looking for "How to change color for .disclosureIndicator
indicator type".
如果有人在这里寻找“如何更改.disclosureIndicator
指示器类型的颜色”。
The answer is you can't. BUT:
答案是你不能。但:
There is a way. Apply a custom image:
有一种方法。应用自定义图像:
let chevronImageView = UIImageView(image: "disclosureIndicatorImage"))
accessoryView = chevronImageView
Additional supporting links:
其他支持链接:
The image can be downloaded from here. How to change color of the image is here.
回答by Nikita Zernov
The best way to do that, i think, is to set accessory to image the following way:
我认为,最好的方法是将附件设置为按以下方式成像:
let image = UIImage(named: "some image.png")
cell.accessoryView = image
回答by zevij
You can also change the cell's tint color from the storyboard (assuming you are using a xib file)
您还可以从故事板更改单元格的色调(假设您使用的是 xib 文件)
回答by Konstantin Kryzhanovsky
Im my case i need to change contentView color of my CustomCell.
我的情况是我需要更改我的 CustomCell 的 contentView 颜色。
Its can be easy making when u override methods :
当你覆盖方法时,它可以很容易地制作:
override func setHighlighted(highlighted: Bool, animated: Bool) {}
and:
和:
override func setSelected(selected: Bool, animated: Bool) {}
But when i add to my customCell :
但是当我添加到我的 customCell 时:
cell?.accessoryType = .DisclosureIndicator
i had a problem when view under DisclosureIndicator
is not change color. Its looks like:
当视图下DisclosureIndicator
没有改变颜色时,我遇到了问题。它看起来像:
So i look on subviews
of CustomCell
and find that DisclosureIndicator
is a button. If change background color of this button u have this
所以我期待上subviews
的CustomCell
,发现DisclosureIndicator
是一个按钮。如果改变这个按钮的背景颜色你有这个
So i try to change background color of superview
of this button. And its work great.
所以我尝试改变superview
这个按钮的背景颜色。它的工作很棒。
Full code of myCustomCell
setHighlighted func :
myCustomCell
setHighlighted func 的完整代码:
override func setHighlighted(highlighted: Bool, animated: Bool) {
if(highlighted){
viewContent.view.backgroundColor = Constants.Colors.selectedBackground
for item in self.subviews {
if ((item as? UIButton) != nil) {
item.superview?.backgroundColor = Constants.Colors.selectedBackground
}
}
} else {
viewContent.view.backgroundColor = Constants.Colors.normalCellBackground
for item in self.subviews {
if ((item as? UIButton) != nil) {
item.superview?.backgroundColor = Constants.Colors.normalCellBackground
}
}
}
}
回答by Kas?m ?zdemir
"How to change color for .disclosureIndicatorindicator type". After much research, I noticed that the image of disclosureIndicator is not an Image but a backgroundImage. I found a solution like this:
“如何更改.disclosureIndicator指标类型的颜色”。经过大量研究,我注意到disclosureIndicator的图像不是Image而是backgroundImage。我找到了这样的解决方案:
import UIKit
class CustomCell: UITableViewCell {
fileprivate func commonInit() {
}
open override func layoutSubviews() {
super.layoutSubviews()
if let indicatorButton = allSubviews.compactMap({ ##代码## as? UIButton }).last {
let image = indicatorButton.backgroundImage(for: .normal)?.withRenderingMode(.alwaysTemplate)
indicatorButton.setBackgroundImage(image, for: .normal)
indicatorButton.tintColor = .red
}
}
}
extension UIView {
var allSubviews: [UIView] {
return subviews.flatMap { [##代码##] + ##代码##.allSubviews }
}
}
回答by Vikas Nayak
cell.contentView.backgroundColor = .grey
cell.contentView.backgroundColor = .grey
cell.backgroundColor = .grey
cell.backgroundColor = .grey