Custom Bindings

Posted on Thu 05 August 2021 in swift

First day back on 100 Days of SwiftUI after a cracking Hacking with Swift: Live! conference. I was inspired by the talks but also by my fellow attendees!

Today we covered some basics for how @State actually wraps a value, and peeked under the hood at how to wrap the @State struct with a custom binding to inject behavior a la didSet or didGet. Paul also introduced ActionSheets, which have since been deprecated in the beta in favor of confirmation dialogs. The only thing I don't like about .confirmationDialog() right now is that I'm likely to forget that titleVisibility defaults to .hidden on my iPhone.

.confirmationDialog("Change background", isPresented: $showingActionSheet, titleVisibility: .visible) {
    Button("Mint") { self.backgroundColor = .mint }
    Button("Purple") { self.backgroundColor = .purple }

Here's a modification to my luminosity sketch from earlier, adding color to the mix.

Selfie with Lines, Color