使用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
无需更改所有列表的外观或做任何奇怪的事情,只需:
.listStyle(GroupedListStyle()),请戴上。ListlistRowInsets部分上的设置为 0。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)
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)
小智 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)
您必须在标题部分的视图上使用与 .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)
在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)
| 归档时间: |
|
| 查看次数: |
1397 次 |
| 最近记录: |