Skip to content

Instantly share code, notes, and snippets.

View juliensagot's full-sized avatar

Julien Sagot juliensagot

View GitHub Profile
@juliensagot
juliensagot / ConditionalEnvironmentOverlay.swift
Created November 8, 2024 10:40
ConditionalEnvironmentOverlay
private struct ConditionalEnvironmentOverlay<Value: Equatable, OverlayContent: View>: ViewModifier {
@Environment(\.self) private var environmentValues
private let keyPath: KeyPath<EnvironmentValues, Value>
private let value: Value
private let overlayContent: OverlayContent
init(
condition: KeyPath<EnvironmentValues, Value>,
equals value: Value,
@juliensagot
juliensagot / CustomButtonStyle.swift
Created October 10, 2024 17:37
SwiftUI custom ButtonStyle example
import SwiftUI
struct CapsuleButtonStyle: ButtonStyle {
enum Appearance {
case plain
case outlined
}
let appearance: Appearance
@juliensagot
juliensagot / ResponsiveButtonStyle.swift
Last active October 6, 2024 22:58
Custom ButtonStyle boilerplate that doesn't take ages to update its `isPressed` state when using spring animations
struct ResponsiveButtonStyle: PrimitiveButtonStyle {
// Use this instead of `Configuration.isPressed`.
@State private var isPressed = false
func makeBody(configuration: Configuration) -> some View {
configuration.label
.scaleEffect(isPressed ? 0.9 : 1)
.animation(.spring, value: isPressed)
._onButtonGesture { isPressed in
self.isPressed = isPressed
@juliensagot
juliensagot / HostingView.swift
Created June 21, 2024 14:22
The missing UIHostingView
import SwiftUI
import UIKit
public final class HostingView: UIView {
public init(@ViewBuilder content: () -> some View) {
super.init(frame: .zero)
let contentView = UIHostingConfiguration(content: content)
.margins(.all, 0)
.makeContentView()
@juliensagot
juliensagot / Interface.swift
Created June 13, 2024 07:49
SwiftUI Interface (from Xcode 16.0 Beta, module names removed)
This file has been truncated, but you can view the full file.
// swift-interface-format-version: 1.0
// swift-compiler-version: Apple Swift version 6.0 effective-5.10 (swiftlang-6.0.0.3.38 clang-1600.0.20.6)
// swift-module-flags: -target arm64e-apple-ios18.0 -enable-objc-interop -enable-library-evolution -swift-version 5 -enforce-exclusivity=checked -Osize -library-level api -enable-experimental-feature Macros -enable-experimental-feature ExtensionMacros -enable-experimental-feature IsolatedAny -enable-bare-slash-regex -user-module-version 6.0.70.5.103 -module-name SwiftUI -package-name SwiftUI
import Accessibility
import Combine
import CoreData
import CoreFoundation
@_exported import CoreGraphics
@_exported import CoreTransferable
import Darwin
@juliensagot
juliensagot / VariableBlurView.swift
Last active November 11, 2024 04:13
SwiftUI variable blur view
import Foundation
import SwiftUI
import UIKit
extension UIBlurEffect {
public static func variableBlurEffect(radius: Double, imageMask: UIImage) -> UIBlurEffect? {
let methodType = (@convention(c) (AnyClass, Selector, Double, UIImage) -> UIBlurEffect).self
let selectorName = ["imageMask:", "effectWithVariableBlurRadius:"].reversed().joined()
let selector = NSSelectorFromString(selectorName)
@juliensagot
juliensagot / Interface.swift
Created February 14, 2024 17:14
SwiftUI Interface (from Xcode 15.3 Beta 3, module names removed)
This file has been truncated, but you can view the full file.
// swift-interface-format-version: 1.0
// swift-compiler-version: Apple Swift version 5.10 (swiftlang-5.10.0.12.5 clang-1500.3.9.1.1)
// swift-module-flags: -target arm64e-apple-ios17.4 -enable-objc-interop -enable-library-evolution -swift-version 5 -enforce-exclusivity=checked -Osize -library-level api -library-level api -enable-experimental-feature Macros -enable-experimental-feature ExtensionMacros -package-name SwiftUI -enable-bare-slash-regex -user-module-version 5.4.37 -module-name SwiftUI
import Accessibility
import Combine
import CoreData
import CoreFoundation
@_exported import CoreGraphics
import CoreText
@_exported import CoreTransferable
@juliensagot
juliensagot / ValuePicker.swift
Last active September 15, 2024 23:13
Custom SwiftUI Picker
import SwiftUI
public struct ValuePicker<SelectionValue: Hashable, Content: View>: View {
private let title: LocalizedStringKey
private let selection: Binding<SelectionValue>
private let content: Content
public init(
_ title: LocalizedStringKey,
selection: Binding<SelectionValue>,
import Foundation
import SwiftUI
// MARK: - Custom Button Style
struct MobileMeButtonStyle: ButtonStyle {
// MARK: Metrics
@ScaledMetric private var cornerRadius = 12
@ScaledMetric private var horizontalLabelPadding = 12
@ScaledMetric private var verticalLabelPadding = 8
import UIKit
import PlaygroundSupport
final class CardView: UIControl {
private let imageView = UIImageView(image: UIImage(named: "Sample"))
override init(frame: CGRect) {
super.init(frame: frame)
layer.cornerRadius = 9