【问题标题】:How to add a drop shadow for a SwiftUI List's Section?如何为 SwiftUI 列表部分添加阴影?
【发布时间】:2022-01-24 04:06:18
【问题描述】:

我有一个 SwiftUI 视图,它在 List 视图中使用了 Section 视图。我要做的是在该部分周围添加一些阴影,以便该部分的内容与主视图的背景明显分开。但是,无论我尝试过什么并在 Google 上搜索过,我都无法在整个部分中显示阴影。作为参考,下面的第一张图片是一个模型,是我试图让我的代码看起来的样子。第二张图片是我的 SwiftUI 视图的夸张版本,可帮助我调试正在发生的事情。如您所知,没有显示阴影。

最后,以下是我的代码。我已经尝试了我能找到的一切,包括更新 UITableView 的Appearance;但是,我认为我正在更新错误的内容。任何帮助,将不胜感激!谢谢!

代码:

struct CatalogView: View {
    @ObservedObject var viewModel: CatalogViewModel

    init(viewModel: CatalogViewModel) {
        self.viewModel = viewModel
    
        UITableView.appearance().backgroundColor = UIColor.cyan
        UITableView.appearance().layer.masksToBounds = false
        UITableView.appearance().layer.shadowColor = UIColor.red.cgColor
        UITableView.appearance().layer.shadowOpacity = 1
        UITableView.appearance().layer.shadowRadius = 100
        UITableView.appearance().layer.shadowOffset = .init(width: 10, height: 10)
    }

    var body: some View {
        NavigationView {
            List {
                Section(LocalizedStringKey("CatalogView.Program.Catalog")) {
                    ForEach(viewModel.programs) {
                        Text($0.name)
                    }
                }
                .shadow(color: Color.red, radius: 25, x: 0, y: 0)
                .headerProminence(.increased)
            }
            .navigationTitle(viewModel.navigationTitle)
            .toolbar {
                ToolbarItem(placement: .navigationBarTrailing) {
                    PlusButton {
                        print("Hi")
                    }
                }
            }
        }
        .navigationViewStyle(.stack)
    }
}

【问题讨论】:

    标签: ios swift uitableview listview swiftui


    【解决方案1】:

    由于UIKit,您必须清除UITableView的背景颜色。

    init(viewModel: CatalogViewModel) {
            self.viewModel = viewModel
        
            UITableView.appearance().backgroundColor = UIColor.clear
            ....................
    }
    

    将阴影代码添加到List 之外而不是Section 之外,然后添加背景颜色以查看

    List {
           Section(LocalizedStringKey("CatalogView.Program.Catalog")) {
             ForEach(viewModel.programs) {
                Text($0.name)
             }
           }
           .headerProminence(.increased)
    }
    .shadow(color: Color.red, radius: 25, x: 0, y: 0) //=====> add this line for shadow 
    .background(Color(UIColor.cyan).ignoresSafeArea()) //=====> add this line for background color
    

    【讨论】:

    • 这太棒了!谢谢!这基本上是我让它工作所需的代码。唯一缺少的是.padding(.top, 1),因为没有此行,这些部分会转到屏幕顶部(位于导航标题和栏的后面)。但是,这样做的一个副作用是 Section 的 Header 也会应用阴影。你知道这是否可以解决吗?
    【解决方案2】:

    试试这个代码

    struct ContentView: View {
    init() { UITableView.appearance().backgroundColor = UIColor.clear }
    
    var body: some View {
        NavigationView {
            List {
                Section(header: Text("Important tasks")) {
                    Text("Hello World")
                    Text("Hello World")
                    Text("Hello World")
                }
                Section(header: Text("Main tasks")) {
                    Text("Hello World")
                    Text("Hello World")
                    Text("Hello World")
                }
                
            }
            .padding()
            .shadow(color: Color.red, radius: 10, x: 5, y: 5)
            .background(Color(UIColor.systemGray4).ignoresSafeArea())
            .navigationTitle("Menu")
        }
    }
    

    }

    【讨论】:

    • 谢谢!我不认为将阴影放在列表本身上。这成功了!唯一不幸的副作用是,当 Section 的 header prominence 设置为 .increased 时:.headerProminence(.increased),阴影颜色也会应用于 header。对我来说幸运的是,我的阴影颜色非常微妙,所以你看不出它的存在。但是,您是否也知道解决此问题的方法?感谢您的帮助!
    • 最欢迎您...您可以尝试这个答案来解决标题阴影问题stackoverflow.com/questions/67460935/list-style-swiftui
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 2014-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多