Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Demo code for "SwiftUI on macOS, how can I make the whole row clickable?"
struct ContentView: View {
@State var selectedSite: String?
let sites = ["Gmail", "Apple", "Google"]
var body: some View {
List {
ForEach(sites, id: \.self) { site in
Button(action: {
self.selectedSite = site
}) {
// TODO: Make this whole row clickable.
HStack {
Text(site)
Spacer() /// 1.
if let selectedSite = self.selectedSite, selectedSite == site {
Button(action: {}) {
Text("Remove")
}
.background(Color.black)
.foregroundColor(.white)
.cornerRadius(6)
}
}
.contentShape(Rectangle()) /// 2.
}
.frame(maxWidth: .infinity, alignment: .leading)
.padding(.horizontal, 10)
.padding(.vertical, 8)
.buttonStyle(PlainButtonStyle())
.font(.system(size: 15))
.foregroundColor(selectedSite == site ? .white : .black)
.background(selectedSite == site ? Color.blue : nil)
.cornerRadius(6)
}
}
}
}
@aheze

This comment has been minimized.

Copy link
Owner Author

@aheze aheze commented Aug 25, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment