|
I have the following code and want to increment the activity count every time I click the button. It turns out, clicking anywhere in the HStack triggers the button click event. Is this a SwiftUI bug or am I missing something here?
|
|
I don't know all the particulars, but after many searches for how to have multiple buttons on a list row, it's expected behavior...whether that's good or bad is another discussion. To achieve your desired behavior, add .buttonStyle(BorderlessButtonStyle()). Also, because it's bit me so many times, I try to avoid setting specific frame sizes. With Image(systemName: "plus") you can use something like .font(Font.system(.body).weight(.bold)) to change the appearance and let SwiftUI work its magic. |
|
@danieleprice1, thank you for the fix and the tip. This behaviour is quite counter-intuitive. |
|
What's funny is that I have another screen where I have two Buttons (separated by a Spacer) in an HStack, and they work as intended. |
|
|
|
Thanks for this solution, this was confusing me. Coming from HTML and CSS to SwiftUI this behavior is not intuitive or expected that a button would overlap everything in the row or HStack without an easy way to set a z-index like you do in HTML. |
|
Thanks! I ran into the same issue (Button at the end of a TextField). Thankfully you guys already solved it! |
SPONSORED Alex is the iOS & Mac developer’s ultimate AI assistant. It integrates with Xcode, offering a best-in-class Swift coding agent. Generate modern SwiftUI from images. Fast-apply suggestions from Claude 3.5 Sonnet, o3-mini, and DeepSeek R1. Autofix Swift 6 errors and warnings. And so much more. Start your 7-day free trial today!
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.