ios - 清除 SwiftUI 中表单部分的背景?

标签 ios swiftui ios14

我正在尝试删除某些部分的白色背景,以便元素正好位于灰色背景上,但我无法删除部分背景或使其变得透明。
这就是我正在尝试的:

struct ContentView: View {
    
    var body: some View {
        Form {
            Section {
                Text("Hello!")
                Button {
                    print("Clicked")
                } label: {
                    Text("Click Me!!")
                }
            }
            Section {
                VStack {
                    Button("Button 1") {}
                    Spacer()
                    Button("Button 2") {}
                }
            }
            .background(Color.clear) // Not doing anything
            Section {
                VStack(alignment: .leading) {
                    Text("Location")
                        .font(.headline)
                    Group {
                        Text("Abc")
                        Text("Abc")
                        Text("Abc")
                    }
                    .font(.caption)
                }
            }
        }
    }
}
这是它的样子:
enter image description here
我尝试添加 .background(Color.clear)SectionVStack ,但没有任何影响。这是如何在 SwiftUI 中实现的?

最佳答案

即使在 SwiftUI 2 Form建立在 UIKit 之上,特别是 UITableView .
您需要删除默认的 UITableViewCell的背景(仅一次,最好在 App init 中):

UITableViewCell.appearance().backgroundColor = UIColor.clear
并使用以下方法更改背景:
Section {
    VStack {
        Button("Button 1") {}
        Spacer()
        Button("Button 2") {}
    }
}
.listRowBackground(Color.clear)

关于ios - 清除 SwiftUI 中表单部分的背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63945992/

相关文章:

objective-c - GHUnit 在 iOS 项目中转换为 ARC 后出现 allocate_pages() 错误

ios - subview 模型的计算属性不会更新父 View 模型的 @ObservedObject

ios - 如何在 SwiftUI 中将自定义字体系列设置为整个应用程序的默认值

ios - 有没有办法解决iOS 14 Widget闪烁问题?

ios - 将 BGTaskScheduler API 与新的 iOS 14 应用程序结合使用。如何在没有 AppDelegate 的情况下注册任务?

ios - 适用于 iOS 26+ StageWebView 的 Adob​​e AIR 不会从 documentsDirectory 加载本地 HTML

ios - 将虚拟对象添加到项目

ios - Xcode 将不需要的文件复制到目标中

ios - 缩放图像以填充后,SwiftUI边缘变得可见

iOS 14 导航栏标题未显示在以编程方式创建的 UINavigationController 中