I have the problem that when the user holds down the navigation link, the color of the space around the respective "cards" changes. Of course, this is not what I want. But as a total SwiftUI beginner, I do not know how to fix it. But I really want to fix this bug because it is not very innovative and irritates the user. So I would appreciate it if someone finds a solution that changes the color of the card instead of the space around it.
I suspect that my problem is because the system considers the space on the sides and the corresponding cards as one list cell. And of course, just like anywhere else in the system, these list cells are changing color when the user holds them down.
And my code:
import SwiftUI
struct ContentView: View {
var body: some View {
UITableView.appearance().separatorStyle = .none
UITableViewCell.appearance().backgroundColor = .none
return NavigationView {
List {
Cards()
.listRowInsets(EdgeInsets(top: 0, leading: 16, bottom: 16, trailing: 16))
Cards()
.listRowInsets(EdgeInsets(top: 0, leading: 16, bottom: 16, trailing: 16))
Cards()
.listRowInsets(EdgeInsets(top: 0, leading: 16, bottom: 16, trailing: 16))
} .listStyle(GroupedListStyle()).padding(.bottom, -32) // GroupedListStyle is needed for the background color
// Navigation bar
.navigationBarTitle(Text("Header"))
.navigationBarItems(
leading:
Button(action: { print("add") }) {
Image(systemName: "plus")
},
trailing:
Button(action: { print("edit") }) {
Text("Edit")
}.disabled(true)
)
}
}
}
// For the Cards on the main screen
struct Cards: View {
var body: some View {
VStack(spacing: 0) {
Image("swiftui")
.resizable()
.aspectRatio(contentMode: .fit)
HStack {
VStack(alignment: .leading, spacing: 0) {
Text("Title")
.font(.title)
.foregroundColor(.primary)
.lineLimit(3)
Text("Subtitle")
.font(.caption)
.foregroundColor(.secondary)
}
.layoutPriority(100)
Spacer()
NavigationLink(destination: EmptyView()) {
EmptyView()
}.opacity(0) // Hiding the default navigation bar chavron
Image(systemName: "chevron.right")
.foregroundColor(.secondary)
.font(Font.body.weight(.semibold))
}
.padding(.all, 16)
.background(Color("CustomCardBackgroundColor")) // This is a custom color set
}
.cornerRadius(10)
.overlay(
RoundedRectangle(cornerRadius: 10)
.stroke(Color(.sRGB, red: 150/255, green: 150/255, blue: 150/255, opacity: 0.1), lineWidth: 1)
)
}
}