这是我的代码:

Form {
  Section {} // regular section no background

  Section { // <--- want this section to have a background color
     Text("Hello")
  }
  .background(Gradient(colors: [.teal, .cyan, .green]).opacity(0.6))

} // Do not want background on form only on an individual section

这只在文本()上添加背景,这是有道理的,但如何让它仅在部分应用背景

推荐答案

您需要使用.listRowBackground并应用渐变作为背景样式.见下文:

Form {
      Section {} // regular section no background

      Section { // <--- want this section to have a background color
         Text("Hello")
      }
      .listRowBackground(LinearGradient(gradient: Gradient(colors: [.teal, .cyan, .green]), startPoint: .leading, endPoint: .trailing))
        
} // Do not want background on form only on an individual section

enter image description here

Swift相关问答推荐

数据不会被ARC删除在swift'

是否有一个Kotlin等价的Swift s @ AddendableReport注释'

如何在Swift中获得与Python相同的HMAC—SHA512签名?

是否在核心数据中使用Uint?

在 SwiftUI 视图中观察 UIViewRepresentable 的 @State var 变化

如何使用Duration.TimeFormatStyle以h m s格式快速格式化时间

如何为我的项目设置生命周期选项?

Xcode 15 Beta中如何使用@Observable?

解码 JSON 时 Swift Struct Decoder 初始化程序错误

SwiftUI:按钮的圆形边缘的边框尺寸加倍

并发执行代码中捕获的 var 的变异

SwiftUI 动画的范围

Swift-如何接受多个(联合)类型作为参数

如何自己快速编写 Hashable 协议

如何有条件地格式化 SwiftUI 代码

为 UIActivityViewController Swift 设置不同的活动项

如何在 iOS Swift 中进行多线程、并发或并行?

使用 phimagemanager 将图像保存到自定义相册?

在 unwind segue 之后执行 push segue

TabView 在切换选项卡时重置导航堆栈