删除/更改SwiftUI列表中的节标题背景色
使用SwiftUI中的一个简单的删除/更改SwiftUI列表中的节标题背景色,swiftui,Swiftui,使用SwiftUI中的一个简单的列表,如何更改/删除节标题的标准背景色 struct ContentView : View { var body: some View { List { ForEach(0...3) { section in Section(header: Text("Section")) { ForEach(0...3) { row in
列表
,如何更改/删除节标题的标准背景色
struct ContentView : View {
var body: some View {
List {
ForEach(0...3) { section in
Section(header: Text("Section")) {
ForEach(0...3) { row in
Text("Row")
}
}
}
}
}
}
在beta 4中,relativeWidth被弃用。代码已更新以反映这一点。 不幸的是,没有设置背景颜色的快速参数。但是,您仍然可以这样做:
导入快捷界面
结构ContentView:View{
var body:一些观点{
名单{
ForEach(0…3){
节(标题:
自定义标题(
名称:“部门名称”,
颜色:颜色。黄色
)
) {
ForEach(0…3){
文本(“行”)
}
}
}
}
}
}
结构CustomHeader:视图{
let name:String
让颜色:颜色
var body:一些观点{
VStack{
垫片()
HStack{
文本(名称)
垫片()
}
垫片()
}
.padding(0).背景(FillAll(颜色:color))
}
}
结构FillAll:视图{
让颜色:颜色
var body:一些观点{
GeometryReader{中的代理
self.color.frame(宽度:proxy.size.width*1.3).fixedSize()
}
}
}
另一种方法是设置标题的框架:
VStack{
名单{
ForEach(0…3){
节(标题:
文本(“节”)
.frame(最小宽度:0,最大宽度:。无穷大,对齐:。前导)
.背景(颜色.蓝色.相对宽度(2))
) {
ForEach(0…3){
文本(“行”)
}
}
}
}
}
我尝试使用上面的自定义头代码,但不幸的是,在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)
}}
可以按如下方式将其添加到列表中的部分:
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)))){
希望这能帮助别人 建议的解决方案一直有效,直到您决定
清除列表标题背景色
列表
标题自定义颜色的更好解决方案:
1.此解决方案影响应用程序中的所有列表部分:(或将其移动到您的AppDelegate
类)
无需更改所有列表的外观或执行任何奇怪的操作,只需:
(可选)如果您不想要粘性标题,请将.listStyle(GroupedListStyle())
放在列表上
将节上的列表行插入设置为0
将部分.backgroundColor
设置为clear
,以删除默认颜色或任何您想要为其着色的颜色
例如:
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
通过使用自定义修改器,我可以使标题变得清晰(在我的例子中变成白色)。我需要使用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())
}
}
在标题部分的视图中,必须使用一个与.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
)
}
但是,在旋转设备时,该功能无法正常工作。iPhone.XR。但是设置一个更大的相对宽度可以修复它。我看到了。更新了相对宽度较大的代码。@当相对宽度为有效选项时,答案被发回。代码已更新。目前,此解决方案在iOS 14.2的横向环境中无法完全工作。它不尊重左侧的安全区域。这不起作用。它为我所做的只是把标题(例如,“标题”)推到边缘,在文本后面做一条蓝色的细带。节标题的灰色仍然存在。起初,直到我意识到我已将.listStyle(GroupedListStyle())留在列表上,这才起作用。一旦我删除了它,这个解决方案也起了作用。我将更新我的答案,以引起更多的注意,但我不想要GroupedListStyle()我想要粘性头我用这个方法得到了很好的结果,但没有使用步骤1,我注意到上面的示例本身没有设置GroupedListStyle。请确保你没有添加.listStyle()列表的修饰符,这将在节标题上强制其自己的视图。这在启动时起作用,但在旋转时会中断,因为宽度没有正确更新。而且它不尊重景观中适当的安全区域大小。谢谢。这是唯一对我有效的解决方案。我必须这样做才能让它起作用。节(标题:文本(“组”).frame(minWidth:0,maxWidth:,alignment:,leading.background(Color.gray))当我这样做时,它基本上工作得很好,但是彩色节标题栏没有到达显示器的两侧,所以我在每一端都有一个小的灰色部分,这相当令人失望。您知道如何解决此问题吗?相对宽度在iOS 14.2上不存在。这在iOS中不起作用14@user832我在iOS 14.2中使用它,非常感谢你让我知道。我在我的分区标题中使用了NavigationLink,这导致了问题。我再次尝试了一些更改,但成功了。在iOS 14.2上不起作用。如果没有GroupedListStyle,这对iOS 14.2没有影响。部分背景色保持默认的灰色。UITableViewHeaderFooterView似乎不再是图片的一部分。谢谢,这对我来说很有用:-)完美。工作得很有魅力。。继续回答,兄弟
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())
}
}
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
)
}