在SwiftUI列表中删除/更改节标题背景颜色

Dav*_*rve 4 swiftui

使用ListSwiftUI中的简单控件,如何更改/删除节标题的标准背景颜色

struct ContentView : View {
    var body: some View {
        List {
            ForEach(0...3) { section in
                Section(header: Text("Section")) {
                    ForEach(0...3) { row in
                        Text("Row")
                    }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

屏幕截图带有灰色部分标题背景

tar*_*all 51

无需更改所有列表的外观或做任何奇怪的事情,只需:

  1. (可选)如果您想要粘性标题.listStyle(GroupedListStyle()),请戴上。List
  2. listRowInsets部分上的设置为 0。
  3. 设置Section.backgroundColortoclear以删除默认颜色,或您想要为它着色的任何颜色。

例子:

List {
    Section(header: HStack {
        Text("Header")
            .font(.headline)
            .foregroundColor(.white)
            .padding()

            Spacer()
    }
    .background(Color.blue)
    .listRowInsets(EdgeInsets(
        top: 0,
        leading: 0,
        bottom: 0,
        trailing: 0))
    ) {
        // your list items
    }
}.listStyle(GroupedListStyle()) // Leave off for sticky headers
Run Code Online (Sandbox Code Playgroud)

带颜色部分标题的示例列表

  • 但我不想要 GroupedListStyle() 我想要粘性标题 (4认同)
  • 这不起作用。它对我所做的只是将标题(例如“标题”)推到边缘,并在文本后面制作一条细蓝色带。节标题的灰色仍然存在。 (3认同)
  • 起初,这不起作用,直到我意识到我已将 .listStyle(GroupedListStyle()) 留在列表中。一旦我删除了它,这个解决方案也起作用了。 (2认同)

FRI*_*DAY 15

建议的解决方案的工作,直到你决定clear你的List标题背景颜色。

List标题自定义颜色的更好解决方案:

1.此解决方案会影响您应用中的所有 List 部分:(或将其移至您的AppDelegate班级)

struct ContentView: View {

init() {
      UITableViewHeaderFooterView.appearance().tintColor = UIColor.clear
    }

var body: some View {
    List {
        ForEach(0 ..< 3) { section in
            Section(header:
                    Text("Section")
            ) {
                ForEach(0 ..< 3) { row in
                    Text("Row")
                }
            }
        }
     }
  }
}
Run Code Online (Sandbox Code Playgroud)

2.使用此解决方案,您可以List为应用中的每个列表自定义标题背景颜色:

struct ContentView: View {
init() {
    UITableViewHeaderFooterView.appearance().tintColor = UIColor.clear
}

var body: some View {
    List {
        ForEach(0 ..< 3) { section in
            Section(header:
                HStack {
                    Text("Section")
                    Spacer()
                }
                .listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))
                .background(Color.blue)

            ) {
                ForEach(0 ..< 3) { row in
                    Text("Row")
                }
            }
        }
     }
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 这在 iOS 14 中不起作用 (4认同)
  • 不适用于 iOS 14.2。 (2认同)

小智 7

我尝试使用上面的自定义标头代码,但不幸的是无法在 beta 6 中使用它。这导致我使用了 ViewModifier:

public struct sectionHeader: ViewModifier{
var backgroundColor:Color
var foregroundColor:Color
public func body(content: Content) -> some View {
    content
    .padding(20)
    .frame(width: UIScreen.main.bounds.width, height: 28,alignment:
    .leading)
    .background(backgroundColor)
    .foregroundColor(foregroundColor)
}}
Run Code Online (Sandbox Code Playgroud)

可以将其添加到列表中的部分,如下所示:

struct ContentView: View {
@ObservedObject var service = someService()
var body: some View {
    NavigationView {
        List{
            ForEach(someService.sections) {section in
                Section(header: Text(section.title).modifier(sectionHeader(backgroundColor: Color(.systemBlue), foregroundColor: Color(.white)))){
Run Code Online (Sandbox Code Playgroud)

希望对某人有所帮助!


小智 7

通过使用自定义修饰符,我能够使标题清晰(在我的情况下变为白色)。我需要使用 listStyle() 修饰符,而上述所有方法对我都不起作用。

希望它可以帮助别人!

List {
    Section(header: HStack {
        Text("Header Text")
            .font(.headline)
            .padding()

            Spacer()
    }
    ) {
ForEach(0...3) { row in
                        Text("Row")
                    }
    }
}.listStyle(GroupedListStyle()).listSeparatorStyle()

public struct ListSeparatorStyleModifier: ViewModifier {
    public func body(content: Content) -> some View {
        content.onAppear {
            UITableView.appearance().separatorStyle = .singleLine
            UITableView.appearance().separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
            UITableViewHeaderFooterView.appearance().tintColor = .clear
            UITableView.appearance().backgroundColor = .clear // tableview background
            UITableViewCell.appearance().backgroundColor = .clear

        }
    }
}

extension View {
    public func listSeparatorStyle() -> some View {
        modifier(ListSeparatorStyleModifier())
    }
}

Run Code Online (Sandbox Code Playgroud)

  • 如果没有 GroupedListStyle,这对 iOS 14.2 没有影响。部分背景颜色保持默认灰色。看来 UITableViewHeaderFooterView 不再是图片的一部分了。 (2认同)

Xax*_*xus 7

您必须在标题部分的视图上使用与 .listRowInsets 结合的矩形

Section(header: headerSectionView) {
    Text("MySection")
}


private var headerSectionView: some View {
    Rectangle()
        .fill(Color.blue) // will make a blue header background
        .listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))
        .overlay(
            Text("My Section Title")
                .padding(.horizontal), // You need this to add back the padding
            alignment: .leading
        )
}

Run Code Online (Sandbox Code Playgroud)


kon*_*iki 5

在beta 4中,不推荐使用relativeWidth。更新了代码以反映这一点。

不幸的是,没有快速的参数可以设置背景颜色。但是,您仍然可以这样做:

在此处输入图片说明

import SwiftUI

struct ContentView : View {
    var body: some View {
        List {
            ForEach(0...3) { section in
                Section(header: CustomeHeader(name: "Section Name", color: Color.yellow)) {
                    ForEach(0...3) { row in
                        Text("Row")
                    }
                }
            }
        }
    }
}

struct CustomeHeader: View {
    let name: String
    let color: Color

    var body: some View {
        VStack {
            Spacer()
            HStack {
                Text(name)
                Spacer()
            }
            Spacer()
        }.padding(0).background(FillAll(color: color))
    }
}

struct FillAll: View {
    let color: Color

    var body: some View {
        GeometryReader { proxy in
            self.color.frame(width: proxy.size.width * 1.3).fixedSize()
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 不过,旋转设备时效果不佳。 (2认同)