Marcos Tanaka
Marcos Tanaka

Reputation: 3336

SwiftUI: How to change List selected item color

iPadOS uses a different selection color when an external keyboard is connected. But the problem is that it doesn't change the text color to white, making it difficult to read:

Comparing the selection color with and without an external keyboard

A simple List with NavigationLink produces this behavior by default:

var body: some View {
    List {
        ForEach(searchResults) { item in
            NavigationLink(destination: ContentDetailView(item: item)) {
                ListItemView(item: item)
            }
        }
    }
}

I tried to improve text legibility by changing all Text colors to white when the cell is selected. But this doesn't work because the text becomes even more unreadable when no external keyboard is connected.

Is there a way to change the selection color when an external keyboard is connected? Or maybe detect when an external keyboard is connected to manually change the text color for this specific case?

Upvotes: 3

Views: 1269

Answers (2)

Rafał Rębacz
Rafał Rębacz

Reputation: 495

Use tint modifier:

List {
//...
}
.tint(.green)

Upvotes: 0

Nairo
Nairo

Reputation: 23

You can use this line to change the selection style in the init of the View

UITableViewCell.appearance().selectionStyle = .none

And then edit the background color when the navigationLink is selected

Upvotes: 1

Related Questions