在SwiftUI中的列表下方删除多余的行分隔符 [英] Remove extra line separators below List in SwiftUI

查看:178
本文介绍了在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在列表下方不会显示多余的分隔符,并且要删除所有分隔符,您需要在ScrollView内使用LazyVStack

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.

SwiftUI的iOS 13的List后面有一个UITableView.因此,请删除

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆