繁体   English   中英

如何使 SwiftUI 列表行背景颜色扩展到整个宽度,包括安全区域之外

[英]How can I make a SwiftUI List row background color extend the full width, including outside of the safe area

在 SwiftUI List中,如何使列表行背景(通过.listRowBackground()设置)扩展视图的整个宽度,甚至在安全区域下? 例如,在宽屏 iPhone(例如 iPhone 12 Pro Max)上横向运行时。 目前,在安全区域开始之前,该单元格在单元格的最左侧显示为白色。

示例代码:

struct ContentView: View {
    var body: some View {
        List {
            Text("Test")
                .listRowBackground(Color(.systemGray3))
        }.listStyle(GroupedListStyle())
    }
}

这会产生如下所示的 UI。 我希望单元格的灰色背景可以扩展设备的整个宽度。

我尝试将.edgesIgnoringSafeArea(.all)添加到Text中,但没有任何区别。

在 iPhone 12 Pro Max 上横向运行的列表,单元格的最左侧显示为白色,与单元格的其余部分为灰色不同

答案是将.edgesIgnoringSafeArea([.leading, .trailing])放在背景Color本身,而不是列表项上。

struct ContentView: View {
    var body: some View {
        List {
            Text("Test").listRowBackground(
                Color(.systemGray3).edgesIgnoringSafeArea([.leading, .trailing])
            )
        }.listStyle(GroupedListStyle())
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM