Skip to content

Instantly share code, notes, and snippets.

View ericlewis's full-sized avatar

Eric Lewis ericlewis

View GitHub Profile
@ericlewis
ericlewis / CodableAppStorage.swift
Last active July 3, 2025 22:13
Using Codable with AppStorage, the easy way!
import Foundation
import SwiftUI
// MARK: Demo
struct Example: RawRepresentable, Codable {
let id: UUID
}
// This is functionally the same as above, but we have a neater type without creating a new protocol.
import SwiftUI
import swiftui_betterpicker
struct GridPickerView: View {
enum Colors: CaseIterable {
case red
case blue
case green
case purple
case yellow
import SwiftUI
import swiftui_betterpicker
struct ListPickerView: View {
enum DayOfWeek: String, CaseIterable {
case monday, tuesday, wednesday, thursday, friday, saturday, sunday
var title: String { rawValue.localizedCapitalized }
}
@State
@ericlewis
ericlewis / !BetterTextFieldStyle.md
Last active February 19, 2022 04:31
Experimental library for creating useful TextFieldStyles in SwiftUI.

Install via SPM per usual. Supports iOS 13+. MIT License.

@ericlewis
ericlewis / StorageProviders.swift
Last active February 17, 2022 16:31
All the speed of State with all the benefits of App/SceneStorage!
import SwiftUI
// MARK: Demo
struct ContentView: View {
@State
private var isActive = false
@SceneStorage("OtherKey")
private var isActiveStock = false
@ericlewis
ericlewis / Toggle+Label.swift
Created February 16, 2022 15:19
An initializer overload which also pushes an environment value on toggle status to the toggle's label.
import SwiftUI
struct IsToggledOnKey: EnvironmentKey {
static let defaultValue: Bool = false
}
extension EnvironmentValues {
fileprivate var _isToggledOn: Bool {
get { self[IsToggledOnKey.self] }
set { self[IsToggledOnKey.self] = newValue }
@ericlewis
ericlewis / Components+Label.swift
Created February 16, 2022 14:49
Initializer overloads to make working with labels easier.
import SwiftUI
extension Toggle where Label == SwiftUI.Label<Text, Image> {
public init(_ titleKey: LocalizedStringKey, systemImage named: String, isOn: Binding<Bool>) {
self.init(isOn: isOn, label: { Label(titleKey, systemImage: named) })
}
public init(_ titleKey: LocalizedStringKey, image named: String, isOn: Binding<Bool>) {
self.init(isOn: isOn, label: { Label(titleKey, image: named) })
}
debug server graph://127.0.0.1:51000/?token=1706701259
== UInt32, 4 bytes ==
(layout #:length 18
(== #:size 4 #:type UInt32))
== EnvironmentValues, 16 bytes ==
(layout #:length 23
(enum #:size 8 #:type Optional<Element>
(case 0
(read 8)))
(enum #:size 8 #:type Optional<Tracker>
@ericlewis
ericlewis / Example.swift
Last active February 11, 2022 00:13
Customize the separator insets on lists in SwiftUI. Only tested on iOS 15. Does not work on SidebarListStyles or interface idioms that aren't pad / phone.
struct ContentView: View {
var body: some View {
List {
ForEach(0..<100) { index in
Text("Index: \(index)")
.listRowSeparatorInsets(
.init(
top: 0,
left: CGFloat(index) * 5,
@ericlewis
ericlewis / Example.swift
Last active February 8, 2022 16:10
PagingView by working with SwiftUI internals. Useful for building "flat" view containers, like a paging view! The paging view itself uses yet another internal type. But serves as a good example for how to build a useful view.
import SwiftUI
struct ContentView: View {
@State
private var show = false
@State
private var selection = 0
var body: some View {