簡體   English   中英

Swift 如何更改列表中 Header 部分的背景顏色?

[英]Swift how to change background color of a Section Header in a List?

我正在嘗試更改此列表部分 Header 的背景顏色,下面只有 colors 文本視圖本身的背景。 我也不想全局更改UITableView.appearance().backgroundColor因為這會影響其他視圖?

 List {
                            Section(header: Text("Summary")
                                        .font(.subheadline)
                                        .fontWeight(.medium)
                                        .background(Color.red).edgesIgnoringSafeArea(.all)
                            ) {
                                Text("\(filterThisMonthsWorkouts(workouts: athlyticDataStore.workouts).count) workouts")
                                    .font(.headline)
                                    .fontWeight(.medium)
                                    .font(Font.body.monospacedDigit())
                                Text("\(TimeFormatters().formatTimeAsDoubleToHoursAndMinutesOnly(duration: filterThisMonthsWorkouts(workouts: athlyticDataStore.workouts).map { $0.duration }.reduce(0, +)))")
                                    .font(.headline)
                                    .fontWeight(.medium)
                                    .font(Font.body.monospacedDigit())
                                Text("\(Int(filterThisMonthsWorkouts(workouts: athlyticDataStore.workouts).map { $0.totalEnergyBurned }.reduce(0, +))) calories burned")
                                    .font(.headline)
                                    .fontWeight(.medium)
                                    .font(Font.body.monospacedDigit())
                            }
                        }
                        .frame(height: sizeCategory > ContentSizeCategory.large ? 200 : 165)
                        .cornerRadius(10)
                        .listStyle(GroupedListStyle())
                        .padding([.horizontal, .top])

這可能會有所幫助:

 Text(...)   
   .frame(maxWidth: .infinity)

編輯:以上適用於表單,但對於列表,它將在前導和尾隨兩側都有一些邊緣,該區域與內容對齊。

把這個放到最后可以填滿header寬度:

            Section(header: Text("hi")
                    .frame(maxWidth: .infinity)
                    .foregroundColor(.green)
                    .background(Color.red)
                    .listRowInsets(.init(top: -10, leading: -10, bottom: -10, trailing: -10)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM