我有一個使用Section視圖中的List視圖的 SwiftUI視圖。我想要做的是在該部分周圍添加一些陰影,以便該部分的內容與主視圖的背景清楚地分開。但是,無論我嘗試過和使用 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)
}
}
uj5u.com熱心網友回復:
試試這個代碼
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")
}
}
}

uj5u.com熱心網友回復:
UITableView由于UIKit ,您必須清除背景顏色。
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/391720.html
