Skip to content

Instantly share code, notes, and snippets.

@kylebshr
kylebshr / HostingScrollViewBounceFix.swift
Last active November 14, 2026 02:54
Fix for SwiftUI ScrollView + .refreshable stuck below the top after refreshing under a collapsible navigation bar (iOS 26/27)
import ObjectiveC
import UIKit
/// Fixes a SwiftUI `ScrollView` with `.refreshable` coming to rest about 60 pt below its top
/// (the refresh control's height) after the refresh ends, until the user touches it again.
///
/// Call `HostingScrollViewBounceFix.install()` once at launch.
///
/// When it happens: a SwiftUI `ScrollView` (not `List`) whose content changes size during the
/// end-of-refresh layout, typically a `LazyVStack` re-estimating rows, under a navigation bar
@kylebshr
kylebshr / AdaptiveCapsule.swift
Last active September 28, 2025 02:20
SwiftUI Shape that will be a capsule up to a maximum corner radius. Useful for capsule UI that should adapt to a rounded rect past a certain scale.
//
// AdaptiveCapsule.swift
// Design
//
// Created by Kyle Bashour on 8/21/25.
//
import SwiftUI
/// Will draw as a capsule up to a maximum corner radius, after which it draws as a rounded rect.
@kylebshr
kylebshr / ConditionalDefaultEntity.swift
Last active February 24, 2026 07:44
How to conditionally build widget configuration parameters based on UserDefaults or other conditions
//
// ConditionalDefaultEntity.swift
// Mercury
//
// Created by Kyle Bashour on 2/10/25.
// Copyright © 2025 Kyle Bashour. All rights reserved.
//
import AppIntents
@kylebshr
kylebshr / StackedHeaderCompositionalLayout.swift
Created November 9, 2023 20:51
UICollectionViewCompositionalLayout subclass that can stack a pinned collection-level header with pinned section-level headers.
class StackedHeaderCompositionalLayout: UICollectionViewCompositionalLayout {
let collectionHeaderKind: String
let sectionHeaderKind: String
init(
section: NSCollectionLayoutSection,
configuration: UICollectionViewCompositionalLayoutConfiguration,
collectionHeaderKind: String,
sectionHeaderKind: String
) {
//
// StandByMargins.swift
//
// Created by Kyle Bashour on 8/29/23.
//
import SwiftUI
extension View {
func standByMargins() -> some View {
@kylebshr
kylebshr / WidgetBackground.swift
Last active June 6, 2023 00:18
Use the new `containerBackground` modifier for widgets if iOS 17 is available, or easily fallback to a `ZStack` with standard padding.
//
// WidgetBackground.swift
//
// Created by Kyle Bashour on 6/5/23.
//
import SwiftUI
struct WidgetBackground<Background: View>: ViewModifier {
private let background: Background
//
// ContentView.swift
// SafeArea
//
// Created by Kyle Bashour on 5/24/23.
//
import SwiftUI
struct ContentView: View {
class ViewController: UIViewController {
let left = ScrollViewController()
let right = ScrollViewController()
override func viewDidLoad() {
super.viewDidLoad()
for child in [left, right] {
addChild(child)
@kylebshr
kylebshr / ViewController.swift
Last active June 7, 2021 23:36
UISheetPresentationController
//
// ViewController.swift
// Sheets
//
// Created by Kyle Bashour on 6/7/21.
//
import UIKit
class ViewController: UIViewController, UISheetPresentationControllerDelegate {
protocol MyProtocol {}
struct MyImplemention: MyProtocol {}
struct MyStruct {
func emptyView() -> some MyProtocol {
MyImplemention()
}