简体   繁体   English

SwiftUI - 列表:在 iOS 13+ 中隐藏分隔线

[英]SwiftUI - List: Hide divider in iOS 13+

I developed a chat screen using SwiftUI.我使用 SwiftUI 开发了一个聊天屏幕。 The current minimum iOS version is 13.0.当前的最低 iOS 版本是 13.0。

To make the chat, I use the element List with ForEach.为了进行聊天,我将元素 List 与 ForEach 一起使用。 The problem is that the list shows the dividers, and I need to hide them:问题是列表显示了分隔符,我需要隐藏它们:

在此处输入图片说明

I tried to hide the style of TableView but nothing works.我试图隐藏 TableView 的样式,但没有任何效果。 Here's the code:这是代码:

struct MessagesView: View {

    var messages: [MessageModel] = []

    init() {
        // 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 {
            ForEach(messages, id: \.messageId) { message in
                Group {
                    if(messPack.user != nil) {
                        ReceivedMessageView(
                            message: message.message,
                            name: message.user?.name,
                            color: message.user?.color)
                    } else {
                        SentMessageView(message: messPack.message)
                    }
                }.listRowInsets(EdgeInsets())
            }
        }
    }
}

I'll be grateful for any help :)我将不胜感激任何帮助:)

Setting minimum iOS version to iOS 13 doesn't mean your code can't run on iOS 14 (and iOS 13 solutions for removing List separators don't work on iOS 14).将最低 iOS 版本设置为 iOS 13 并不意味着您的代码不能在 iOS 14 上运行(用于删除列表分隔符的 iOS 13 解决方案不适用于 iOS 14)。

You need to use if #available(iOS 14, *) to specify which code use for which iOS version:您需要使用if #available(iOS 14, *)来指定哪个代码用于哪个 iOS 版本:

struct MessagesView: View {
    var messages: [MessageModel] = []

    init() {
        UITableView.appearance().tableFooterView = UIView()
        UITableView.appearance().separatorStyle = .none
    }

    var body: some View {
        List {
            ForEach(messages, id: \.messageId) { message in
                Group {
                    if #available(iOS 14, *) {
                        messageView(for: message)
                            .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading)
                            .listRowInsets(EdgeInsets())
                            .background(Color.white)
                    } else {
                        messageView(for: message)
                    }
                }
            }
        }
    }

    @ViewBuilder
    func messageView(for message: MessageModel) -> some View {
        if messPack.user != nil {
            ReceivedMessageView(
                message: message.message,
                name: message.user?.name,
                color: message.user?.color
            )
        } else {
            SentMessageView(message: messPack.message)
        }
    }
}

Removing List separators:删除List分隔符:

You can play around with some of the ListView styles.您可以使用一些 ListView 样式。 I think SidebarListStyle doesn't have any lines.我认为 SidebarListStyle 没有任何线条。 Otherwise, you'll probably have to use a ScrollView and/or VStack instead of a List.否则,您可能必须使用 ScrollView 和/或 VStack 而不是 List。

    List {
        Text("ONE")
        Text("TWO")
        Text("THREE")
    }
    .listStyle(SidebarListStyle())

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

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