简体   繁体   中英

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:

比较使用和不使用外部键盘的选择颜色

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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