gpt4 book ai didi

SwiftUI 删除 NavigationView 列表顶部的间距

转载 作者:行者123 更新时间:2023-12-03 08:03:32 28 4
gpt4 key购买 nike

我有一个简单的 SwiftUI View ,它使用列表中的部分:

struct NewView: View {
var body: some View {
NavigationView {
List {
Section("Title") {
ForEach((1...10), id: \.self) {
Text("\($0)")
}
}
}
.navigationTitle("Title")
}
}
}

运行时(在 iOS 15 中),顶部会留下巨大的间隙(与没有部分标题时相比):

With Section Without Section

如何减小此间距?

我尝试过一些黑客解决方案,例如:

UITableView.appearance().contentInset.top = -35

但这使得滚动 ScrollView 出现错误,我希望有更好的方法。

最佳答案

只需删除部分标题

    NavigationView {
List {
Section { // << here !!
ForEach((1...10), id: \.self) {
Text("\($0)")
}
}
}
.border(.red) // << for test
.navigationTitle("Title")
}

使用 Xcode 13.4/iOS 15.5 进行测试。

demo enter image description here

添加了带有标题标题的变体 - 只需删除默认标题的插图

    List {
Section {
ForEach((1...10), id: \.self) {
Text("\($0)")
}
} header: {
Text("Title")
.listRowInsets(EdgeInsets()) // << here !!
}
}

关于SwiftUI 删除 NavigationView 列表顶部的间距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73154283/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com