Skip to content

Instantly share code, notes, and snippets.

View ryanlintott's full-sized avatar

Ryan Lintott ryanlintott

View GitHub Profile
@ryanlintott
ryanlintott / AutoLayoutTestApp.swift
Last active March 8, 2024 04:33
A SwiftUI example of an alert window that would be easy to implement using AutoLayout. This implementation uses onSizeChange from FrameUp https://github.com/ryanlintott/FrameUp
//
// AutoLayoutTestApp.swift
// AutoLayoutTest
//
// Created by Ryan Lintott on 2024-03-07.
//
import FrameUp
import SwiftUI
@ryanlintott
ryanlintott / ShrinkingLabel.swift
Last active February 21, 2024 18:31
An example of a label that grows and shrinks in overall size and frame height. Requires FrameUp https://github.com/ryanlintott/FrameUp
//
// ShrinkingLabel.swift
// FrameUpExample
//
// Created by Ryan Lintott on 2024-02-21.
//
import FrameUp
import SwiftUI
@ryanlintott
ryanlintott / KeyboardButtonStyle.swift
Last active February 25, 2024 01:15
A SwiftUI ButtonStyle that looks like an iOS keyboard key.
//
// KeyboardButtonStyle.swift
// Wordhord
//
// Created by Ryan Lintott on 2024-02-06.
//
import ShapeUp
import SwiftUI
@ryanlintott
ryanlintott / ThemeListSelection.swift
Created February 12, 2024 20:54
A SwiftUI List where a single item is selected across Sections.
import SwiftUI
struct Theme: Identifiable {
let id: UUID
let name: String
}
extension Theme {
static let blue = Self(id: UUID(), name: "Blue")
static let red = Self(id: UUID(), name: "Red")
@ryanlintott
ryanlintott / ContentView.swift
Last active May 13, 2024 07:20
A SwiftUI environment value for the keyboard height that updates with animation. This is useful when you want a specific view in a stack to stick to the bottom of the keyboard when the keyboard moves up.
import SwiftUI
struct ContentView: View {
var body: some View {
KeyboardAvoidingWithOffset()
.keyboardHeightEnvironmentValue()
}
}
struct KeyboardAvoidingWithOffset: View {
@ryanlintott
ryanlintott / GridAccessibilityTest.swift
Created October 23, 2023 19:20
SwiftUI Grid by default does not have a way to add accessibility labels to an entire row. This is an example workaround to provide that feature. It also provides a background view for each row but the colours can be set to Color.clear if you only want the accessibility label.
//
// GridAccessibilityTest.swift
// FrameUpExample
//
// Created by Ryan Lintott on 2023-10-22.
//
import FrameUp
import SwiftUI
@ryanlintott
ryanlintott / WidgetConfiguration+disfavoredLocationsIfAvailable.swift
Created September 11, 2023 18:49
Sets the disfavored locations for a widget if the option is available (iOS 17 and up). Otherwise returns the same configuration.
//
// WidgetConfiguration+extensions.swift
//
//
// Created by Ryan Lintott on 2023-09-11.
//
import SwiftUI
import WidgetKit
@ryanlintott
ryanlintott / NavigationStackWithoutLine.swift
Created July 23, 2023 02:26
A SwiftUI NavigationStack with a custom background color and no line below the title.
struct NavigationStackWithoutLine: View {
let backgroundColor = Color.green
var body: some View {
NavigationStack {
ScrollView {
Text("Hello, world!")
.frame(maxWidth: .infinity)
.foregroundColor(.white)
}
.navigationTitle("Hello")
@ryanlintott
ryanlintott / Text+joined.swift
Last active July 10, 2023 15:13
An extension to Text that allows you to join an array of Text into a single Text with optional separators.
extension Collection where Element == Text {
func joined() -> Text {
reduce(into: Text("")) {
$0 = $0 + $1
}
}
func joined(separator: String) -> Text {
joined(separator: Text(separator))
}
@ryanlintott
ryanlintott / PreviewState.swift
Last active June 11, 2023 02:45
A wrapper to create any number of arbitrary state variables using parameter packs for testing bindable values in a preview.
import SwiftUI
struct PreviewState<each T, Content: View>: View {
@State private var value: (repeat each T)
var content: (Binding<(repeat each T)>) -> Content
init(
_ value: repeat each T,
@ViewBuilder content: @escaping (Binding<(repeat each T)>) -> Content
) {