2

I developed a chat screen using SwiftUI. The current minimum iOS version is 13.0.

To make the chat, I use the element List with ForEach. The problem is that the list shows the dividers, and I need to hide them:

enter image description here

I tried to hide the style of TableView but nothing works. 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 :)

6
  • 1
    minimum iOS version is 13.0 - but on which version do you run it? Commented Nov 7, 2020 at 18:22
  • 1
    Is there some reason you need it to be in a list? Wouldn't a ScrollView accomplish the same thing without the dividers? List is supposed to have dividers, where ScrollView is plain. Commented Nov 7, 2020 at 18:22
  • 1
    Does this answer your question stackoverflow.com/a/62598818/12299030? Commented Nov 7, 2020 at 18:32
  • 1
    Consider use github.com/siteline/SwiftUI-Introspect Commented Nov 7, 2020 at 18:33
  • Thank you for all your answers. I'm new at SwiftUi. I thought that List manage the memory very well when the list is very long (lazy loading, etc.). Does ScrollView do the same thing ? Commented Nov 8, 2020 at 13:59

2 Answers 2

0

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).

You need to use if #available(iOS 14, *) to specify which code use for which iOS version:

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:

Sign up to request clarification or add additional context in comments.

Comments

0

You can play around with some of the ListView styles. I think SidebarListStyle doesn't have any lines. Otherwise, you'll probably have to use a ScrollView and/or VStack instead of a List.

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

1 Comment

Hi, thank you for your answer. Sadly, this option is only available for iOS 14+.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.