【问题标题】:How can I clear default highlighted gray color on list item in SwiftUI?如何在 SwiftUI 中清除列表项上的默认突出显示灰色?
【发布时间】:2020-03-31 13:18:06
【问题描述】:

我无法更改 List 项目的突出显示颜色,在 UIKit Id 中轻松更改 TableViewCellSelection 属性元素 DefaultNone .我找不到这样一个简单交互的解决方案,有什么我遗漏的吗?

谢谢。

编辑:我试过了,但没用。

NavigationLink(destination: DetailView(detail: self.viewModel.characters.results[index])) {  

CharacterCell(character: self.viewModel.characters.results[index])
                                    .buttonStyle(PlainButtonStyle())

}

【问题讨论】:

标签: ios swift uikit swiftui swiftui-list


【解决方案1】:

将代码添加到 init()

   init() {
        // Set selected (onTapped) background color of cell.
        let color = UIView()
        color.backgroundColor = UIColor.red
        UITableViewCell.appearance().selectedBackgroundView = color
    }

例如:

import SwiftUI

struct SomeView: View {

    init() {
        // Set selected (onTapped) background color of cell.
        let color = UIView()
        color.backgroundColor = UIColor.red
        UITableViewCell.appearance().selectedBackgroundView = color
    }

    var body: some View {

        // Your List/ForEach code
        Text(/"Hello, World!")
    }
}

我注意到 swiftUI 预览没有显示更改,但是在模拟器中或在设备上运行时您会看到效果。

在您的情况下,您将设置 UIColor.clear ,这应该会根据需要消除灰色。

【讨论】:

    猜你喜欢
    • 2015-07-26
    • 2019-10-25
    • 2019-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-27
    相关资源
    最近更新 更多