Skip to content

Instantly share code, notes, and snippets.

View Amzd's full-sized avatar
🚀

Casper Zandbergen Amzd

🚀
View GitHub Profile
@Amzd
Amzd / Spacer.swift
Created October 17, 2019 09:00
Tap gesture on Spacer
extension Spacer {
/// https://stackoverflow.com/a/57416760/3393964
public func onTapGesture(count: Int = 1, perform action: @escaping () -> Void) -> some View {
ZStack {
Color.black.opacity(0.001).onTapGesture(count: count, perform: action)
self
}
}
}
@Amzd
Amzd / SheetHeight.swift
Created October 17, 2019 10:37
SwiftUI Sheet with height value
//
// SheetHeight.swift
// Created by Casper Zandbergen on 17/10/2019.
//
import SwiftUI
import UIKit
extension View {
/// Presents a sheet.
extension Array where Element: Identifiable {
public subscript(id: Element.ID) -> Element? {
first { $0.id == id }
}
}
// let arrayOfIdentifiables = []
// let itemWithId = arrayOfIdentifiables[id]
@Amzd
Amzd / PreferenceUIHostingController.swift
Last active September 8, 2023 12:14
PreferenceUIHostingController. Adds hiding home indicator and deferring system edge gestures to SwiftUI. (Don't work at the same time but I think that's normal?)
extension View {
/// Controls the application's preferred home indicator auto-hiding when this view is shown.
func prefersHomeIndicatorAutoHidden(_ value: Bool) -> some View {
preference(key: PreferenceUIHostingController.PrefersHomeIndicatorAutoHiddenPreferenceKey.self, value: value)
}
/// Controls the application's preferred screen edges deferring system gestures when this view is shown. Default is UIRectEdgeNone.
func edgesDeferringSystemGestures(_ edge: UIRectEdge) -> some View {
preference(key: PreferenceUIHostingController.PreferredScreenEdgesDeferringSystemGesturesPreferenceKey.self, value: edge)
}
@Amzd
Amzd / NestedPublished.swift
Last active July 20, 2020 11:12
Moved to repo so you can use it in Swift Package Manager: https://github.com/Amzd/NestedPublished
/// Just like @Published this sends willSet events to the enclosing ObservableObject's ObjectWillChangePublisher
/// but unlike @Published it also sends the wrapped value's published changes on to the enclosing ObservableObject
@propertyWrapper @available(iOS 13.0, *)
public struct NestedPublished<Value: ObservableObject> where Value.ObjectWillChangePublisher == ObservableObjectPublisher {
public init(wrappedValue: Value) {
self.wrappedValue = wrappedValue
self.cancellable = nil
startListening(to: wrappedValue)
}
@Amzd
Amzd / TextFieldFocusableArea.swift
Last active August 6, 2021 14:48
SwiftUI TextField has a very small tappable area and there is no simple way to make that area bigger. This is a solution using Introspect (https://github.com/siteline/SwiftUI-Introspect/) Stack Overflow question: https://stackoverflow.com/questions/56795712/swiftui-textfield-touchable-area/62089790#62089790
extension View {
public func textFieldFocusableArea() -> some View {
TextFieldButton { self.contentShape(Rectangle()) }
}
}
fileprivate struct TextFieldButton<Label: View>: View {
init(label: @escaping () -> Label) {
self.label = label
}
@Amzd
Amzd / View+NSCursor.swift
Last active February 9, 2024 17:34
Set the cursor that is displayed when hovering a View. (macOS, SwiftUI)
import SwiftUI
extension View {
/// https://stackoverflow.com/a/61985678/3393964
public func cursor(_ cursor: NSCursor) -> some View {
self.onHover { inside in
if inside {
cursor.push()
} else {
NSCursor.pop()
@Amzd
Amzd / HierarchyDescription.swift
Last active May 16, 2023 19:48
Description that can be used with any type that has recursive children
public protocol HierarchyDescription {
associatedtype Child: HierarchyDescription
var children: [Child] { get }
var description: String { get }
}
extension HierarchyDescription {
public var hierarchyDescription: String {
var description = self.description
for child in children {
@Amzd
Amzd / AnyPublisher.swift
Created December 7, 2020 11:26
Empty and constant AnyPublisher extensions
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
extension AnyPublisher {
static var empty: Self {
Empty(completeImmediately: false).eraseToAnyPublisher()
}
static func constant(_ value: Output) -> Self where Failure == Never {
Just(value).eraseToAnyPublisher()
}
}
@Amzd
Amzd / Publisher+void.swift
Last active January 16, 2021 22:35
Void publisher result
extension Publisher {
func void() -> Publishers.Map<Self, Void> {
self.map { _ in () }
}
}