【发布时间】:2010-11-26 03:39:55
【问题描述】:
我想将附件视图图标设置为白色,类似于在黑色 TabBar 中自动将图标设置为白色的方式,但对我来说如何做到这一点并不是很明显。有人有这方面的经验吗?
【问题讨论】:
我想将附件视图图标设置为白色,类似于在黑色 TabBar 中自动将图标设置为白色的方式,但对我来说如何做到这一点并不是很明显。有人有这方面的经验吗?
【问题讨论】:
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
}
【讨论】:
在您的自定义单元类中
override func awakeFromNib() {
super.awakeFromNib()
tintColor = UIColor.red
}
或
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
}
【讨论】:
实际上正确的答案(尽管为时已晚)是将您的单元格 tintColor 属性设置为白色 UIColor 对象,如下所示:
UITableViewCell *cell = [UITableViewCell new]; // or other instantiation...
[cell setTintColor:[UIColor whiteColor]];
完全子类化附件视图也是可能的,但问题是要求使单元格附件按钮变白,所以这应该是正确的答案(至少对于iOS7)。
注意:更正了属性名称,还请注意它实际上不适用于指标配件类型
【讨论】:
结合@BharathRao、@Hemang 和@Kiattisak Anochitarom 想法的解决方案
这似乎是 iOS 13+ 中的一个问题(更改 tintColor 不会有任何区别),Apple Developer Forums 上此页面的浏览量超过 2000
(https://forums.developer.apple.com/thread/121472)
我使用@BharathRao 和@Hemang 的想法的解决方案是向UITableViewCell 添加一个扩展名为eg:addDisclosureIndicator
如果有多个表格和情节提要,使用扩展程序将使应用程序中的内容更加统一。
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
}
}
打电话
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
cell.addDisclosureIndicator()
return cell
}
希望这有帮助。
【讨论】:
您最好的办法是制作一张您想要放入其中的图像,然后将附件视图设置为该图像。这里是Apple's Official Documentation for UITableViewCells。
【讨论】:
Swift 3.0
两种方式:
UIImageView 作为表格视图单元格的accessoryView。如果您使用的是系统附件类型,如Disclosure Indicator箭头图标(>),您可以更改UIImageViewtintColor来实现。如下:
cell?.subviews.forEach({
if let btn = $0 as? UIButton {
btn.subviews.forEach({
if let imageView = $0 as? UIImageView {
let image = imageView.image?.withRenderingMode(.alwaysTemplate)
imageView.image = image
imageView.tintColor = UIColor.red
}
})
}
})
【讨论】:
UIImageView *checkmark = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"coloredCheckmark.png"]];
cell.accessoryView = checkmark;`
享受
【讨论】:
为表格视图设置色调颜色为我解决了这个问题。
【讨论】:
如果您为附件视图使用自定义图像/按钮,则需要更改按钮的色调颜色,例如
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;
【讨论】:
我会做的最简单的方法是“自己从图像创建配件”并使用模板颜色选项更改颜色,或者只使用具有所需颜色的图像。
【讨论】: