【问题标题】:SwiftUI: Make inset List background transparent?SwiftUI:使插入列表背景透明?
【发布时间】:2021-04-12 00:58:32
【问题描述】:

如何将List 的灰色背景设置为完全透明以让红色通过?

struct ContentView: View {
    var body: some View {
        NavigationView {
            ZStack {
                Color.red
                    .ignoresSafeArea()
                VStack {
                    HStack {
                        Text("Faux Title")
                            .font(.system(.largeTitle, design: .rounded))
                            .fontWeight(.heavy)
                        Spacer()
                        Button(action: {
                            // settings
                        }, label: {
                            Image(systemName: "gearshape.fill")
                                .font(.system(.title2))
                        })
                    }
                    .padding()
                    .navigationBarHidden(true)
                    
                    List {
                        Text("One")
                        Text("Two")
                        Text("Three")
                    }
                    .listStyle(InsetGroupedListStyle())
                }
            }
        }
    }
}

【问题讨论】:

    标签: swiftui swiftui-list


    【解决方案1】:

    在视图改变UITableView.appearance的init()中: 我们在 SwiftUI 中使用最多的东西,例如 List 或 NavigationView,. .它们不是纯 SwiftUI,它们来自 UIKit,因此我们使用 UIKit 代码类型来改变外观。如果你记得我们应该在 UIKit 中使用这样的代码。当我们在 init 中使用 UITableView 时,我们正在访问 UITableView UIKit 类,这意味着此更改将适用于我们项目中使用 List 的任何 View。

    最好的方法是你正在做的方式,设置透明颜色,然后在正文中从 SwiftUI 更改颜色!这是最方便的方式。


    struct ContentView: View {
        
        init() { UITableView.appearance().backgroundColor = UIColor.clear }   // <<: here!
        
        var body: some View {
            NavigationView {
                ZStack {
                    Color.red
                        .ignoresSafeArea()
                    VStack {
                        HStack {
                            Text("Faux Title")
                                .font(.system(.largeTitle, design: .rounded))
                                .fontWeight(.heavy)
                            Spacer()
                            Button(action: {
                                // settings
                            }, label: {
                                Image(systemName: "gearshape.fill")
                                    .font(.system(.title2))
                            })
                        }
                        .padding()
                        .navigationBarHidden(true)
                        
                        List {
                            Text("One")
                            Text("Two")
                            Text("Three")
                        }
                        .listStyle(InsetGroupedListStyle())
                    }
                }
            }
        }
    }
    

    【讨论】:

    • 是的!这行得通!我想知道您是否可以通过简要说明其工作原理及其实际作用来更新您的答案。仍然试图围绕这些 SwiftUI 的一些东西。谢谢!
    • 对不起,我解释了它,它不是纯粹的 SwiftUI,这就是为什么我们应该使用一些 UIKit 代码。
    猜你喜欢
    • 2022-08-12
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多