SwiftUI Table如何将所有列foregroundStyle设为primary?
SwiftUI Table统一设置所有列文字样式为primary的两种方法
因为用TableColumn的value绑定方式无法直接修改前景样式,你可以用以下两种方式实现:
方法一:自定义列内容
放弃直接用value参数,改用content闭包手动生成文本并设置样式,这种方式既能单独控制每一列,也能统一设置样式:
Table(studentRecords, selection: $selectedStudentID) { TableColumn("First Name") { record in Text(record.firstName) .foregroundStyle(.primary) } TableColumn("Last Name") { record in Text(record.lastName) .foregroundStyle(.primary) } }
方法二:全局设置环境变量
给整个Table设置foregroundStyle环境值,所有列的文本会自动继承这个样式,代码更简洁:
Table(studentRecords, selection: $selectedStudentID) { TableColumn("First Name", value: \.firstName) TableColumn("Last Name", value: \.lastName) } .environment(\.foregroundStyle, Color.primary)
内容的提问来源于stack exchange,提问作者alexc0082
相关产品推荐
相关产品推荐

