Skip to content

Instantly share code, notes, and snippets.

@aheze
Created August 25, 2021 02:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aheze/d062f9fae42b8c2e62084606e90dcf94 to your computer and use it in GitHub Desktop.
Save aheze/d062f9fae42b8c2e62084606e90dcf94 to your computer and use it in GitHub Desktop.
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
Copy link
Author

aheze commented Aug 25, 2021

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