Swift -如何更改附件类型(disclosureIndicator)的颜色?

6g8kf2rb  于 2022-12-10  发布在  Swift
关注(0)|答案(7)|浏览(151)

我对单元格的accessoryType有疑问。我正在使用一个disclosureIndicator的单元格作为accessoryType,我想改变它的颜色,但我不能。有人知道这是一个错误还是苹果强迫我使用灰色吗?实际上我可以改变其他accessoryType的颜色。
我的代码如下所示:

let cell = tableView.dequeueReusableCell(withIdentifier: "identifier", for: indexPath) as! customCell
cell.tintColor = UIColor.red
cell.accessoryType = .disclosureIndicator

我的箭头仍然是灰色的。但是如果我使用复选标记accessoryType,它会变成红色。
有什么方法可以解决这个问题吗?或者我必须使用彩色图像吗?

b1uwtaje

b1uwtaje1#

你可以这样做

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
    cell.tintColor = UIColor.white

    let image = UIImage(named: "Arrow.png")
    let checkmark  = UIImageView(frame:CGRect(x:0, y:0, width:(image?.size.width)!, height:(image?.size.height)!));
    checkmark.image = image
    cell.accessoryView = checkmark

    let object = objects[indexPath.row] as! NSDate
    cell.textLabel!.text = object.description
    return cell
}

示例箭头图像

第一次

输出

zzlelutf

zzlelutf2#

使用SF符号

let image = UIImage(systemName: "chevron.right")
let accessory  = UIImageView(frame:CGRect(x:0, y:0, width:(image?.size.width)!, height:(image?.size.height)!))
accessory.image = image

// set the color here
accessory.tintColor = UIColor.white
cell.accessoryView = accessory
r7knjye2

r7knjye23#

针对Swift 4.2进行了更新,并附上了图片

cell.accessoryType = .disclosureIndicator
    cell.tintColor = .black
    let image = UIImage(named:"disclosureArrow")?.withRenderingMode(.alwaysTemplate)
    if let width = image?.size.width, let height = image?.size.height {
        let disclosureImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: width, height: height))
        disclosureImageView.image = image
        cell.accessoryView = disclosureImageView
    }

您可以使用的图像:

第一次

可能的外观:

rqmkfv5c

rqmkfv5c4#

波纹管代码为Swift 3.0代码,将根据tintColor更改附件类型颜色。

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    cell.textLabel?.text = "SOME TITLE GOES HERE"

    cell.accessoryType = .disclosureIndicator

    cell.tintColor = UIColor.blue

    let image = UIImage(named:"arrow1")?.withRenderingMode(.alwaysTemplate)

    let checkmark  = UIImageView(frame:CGRect(x:0, y:0, width:(image?.size.width)!, height:(image?.size.height)!));
    checkmark.image = image
    cell.accessoryView = checkmark

    return cell
}

qyyhg6bp

qyyhg6bp5#

Swift 5.扩展样式;)

extension UITableViewCell {
    func setupDisclosureIndicator() {
        accessoryType = .disclosureIndicator
        let imgView = UIImageView(frame: CGRect(x: 0, y: 0, width: 7, height: 12))
        imgView.contentMode = .scaleAspectFit
        imgView.image = UIImage(named: "your_icon_name")
        accessoryView = imgView
    }
}
ev7lccsx

ev7lccsx6#

斯威夫特5、iOS 15和Xcode 13

下面是一个使用SF符号的扩展,因此您会看到一个类似于默认显示指示符的V形:

extension UITableViewCell {
    func addCustomDisclosureIndicator(with color: UIColor) {
        let button = UIButton(frame: CGRect(x: 0, y: 0, width: 10, height: 15))
        let symbolConfig = UIImage.SymbolConfiguration(pointSize: 15, weight: .regular, scale: .large)
        let symbolImage = UIImage(systemName: "chevron.right",
                                  withConfiguration: symbolConfig)
        button.setImage(symbolImage?.withTintColor(color, renderingMode: .alwaysOriginal), for: .normal)
        button.tintColor = color
        self.accessoryView = button
    }
}

您可以像这样使用它:

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    cell.addCustomDisclosureIndicator(with: .white) // Here your own color
    return cell
}
nvbavucw

nvbavucw7#

斯威夫特5和iOS 11-15

一些答案的组合

extension UITableViewCell {
    func addCustomDisclosureIndicator(with color: UIColor) {
        accessoryType = .disclosureIndicator
        let disclosureImage = UIImage(named: "arrow_right")?.withRenderingMode(.alwaysTemplate)
        let imageWidth = (disclosureImage?.size.width) ?? 7
        let imageHeight = (disclosureImage?.size.height) ?? 12
        let accessoryImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: imageWidth, height: imageHeight))
        accessoryImageView.contentMode = .scaleAspectFit
        accessoryImageView.image = disclosureImage
        accessoryImageView.tintColor = color
        accessoryView = accessoryImageView
    }
}

相关问题