在 SwiftUI 中删除列表下方的额外行分隔符 [英] Remove extra line separators below List in SwiftUI
问题描述
我已经创建了一个简单的 List
如下,但它下面有额外的分隔符.
I've created a simple List
as below, but there are extra separators below it.
List {
Text("Item 1")
Text("Item 2")
Text("Item 3")
}
结果:
我尝试将 List 嵌入 VStack
并添加 Spacer()
如下代码,但它无法正常工作.它删除了大约一半的空单元格.
I've tried embedding the List inside a VStack
and adding Spacer()
like below code but it's not working properly. It removes about half of the empty cells.
VStack{
List {
Text("Item 1")
Text("Item 2")
Text("Item 3")
}
Spacer()
}
如何在 SwiftUI 中删除这些额外的分隔符?
How would I remove these extra separators in SwiftUI?
推荐答案
iOS 14:
iOS 14 默认不显示额外的分隔符列表下方,要删除所有分隔符,您需要在其中使用 LazyVStack
一个 ScrollView
代替.(因为 iOS 不再支持 SwiftUI 列表的外观).
iOS 14:
iOS 14 doesn't show extra separators below the list by default and to removing all separators, you need to use a LazyVStack
inside a ScrollView
instead. (because iOS is NOT supporting appearance for SwiftUI lists anymore).
⚠️ 此方法已弃用且不适用于 iOS 14
⚠️ This method is deprecated and it's not working from iOS 14
不需要Section
或.grouped
样式!
在 iOS 13 的 SwiftUI 的 List
后面有一个 UITableView
.所以要删除
No need for Section
or .grouped
style!
There is a UITableView
behind SwiftUI's List
for iOS 13. So to remove
你需要一个 tableFooterView
并删除.请注意默认情况下,iOS 14 不会在列表下方显示额外的分隔符.
you need a tableFooterView
and to remove.
Note that iOS 14 doesn't show extra separators below the list by default.
你需要 separatorStyle
为 .none
init() {
if #available(iOS 14.0, *) {
// iOS 14 doesn't have extra separators below the list by default.
} else {
// To remove only extra separators below the list:
UITableView.appearance().tableFooterView = UIView()
}
// To remove all separators including the actual ones:
UITableView.appearance().separatorStyle = .none
}
var body: some View {
List {
Text("Item 1")
Text("Item 2")
Text("Item 3")
}
}
注意它消除了所有表格/列表的分隔符.因此,您可以根据需要在 onAppear()
等方法中切换它.
Note that it eliminates all tables/lists's separators. So you can toggle it in a methods like onAppear()
or etc. as you wish.
这篇关于在 SwiftUI 中删除列表下方的额外行分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!