Hello, hope you doing fantastic.
I got master-detail view app where UI is build with SwiftUI.
Master view is a List
of NavigationLink
's. Data for each NavigationLink is populated via Core Data fetch request.
import CoreData
import SwiftUI
struct FilteredList: View {
var fetchRequest: FetchRequest<Book>
var books: FetchedResults<Book> {
fetchRequest.wrappedValue
}
@Environment(\.managedObjectContext) var moc
var body: some View {
List {
ForEach(books, id: \.self) { book in
NavigationLink(destination: BookDetailView(book: book)) {
HStack {
Text(book ?? "Book Default Title")
}
}
}
}
}
}
What I am trying to achieve in Mac Catalyst version of the app: I need to change background of currently selected NavigationLink
in List
to say, for example, system green color. Because in ligh mode on the Mac list row has black text and default blue background, so the content is hard to read. Please, see the screenshot:
I tried to set @State private var selectedBook: Book?
and add
.onTapGesture {
self.selectedBook = book
print("Debug: tap")
}
to NavigationLink
, and then change background for NavigationLink
this way:
.listRowBackground(self.selectedBook == book ? Color.green : Color.clear)
But print statement work less than half of the time, I don't think this is a right way.
How can I change background color of selected item in List
? Any help is much appreciated. Thanks.