Skip to content

Instantly share code, notes, and snippets.

View hannesoid's full-sized avatar

Hannes Oud hannesoid

View GitHub Profile
@hannesoid
hannesoid / Locked.swift
Last active December 3, 2019 11:24
Atomically locking property wrapper for swift
//
// Locked.swift
//
//
// Created by Hannes Oud on 29.11.19.
// Helpful reading https://www.vadimbulavin.com/swift-atomic-properties-with-property-wrappers/
import Foundation
/// Atomically locks a property using `os_unfair_lock`
@hannesoid
hannesoid / iOS15-UITableView-DuplicateCellUseIssue.md
Last active June 22, 2023 14:26
iOS15 UITableView Duplicate Cell Usage Issue

This only happens on iOS 15 and only when building with Xcode 13. UITableView with (non-diffable) UITableViewDataSource and just one cell type.

Steps

The set up where the issues arises is, we have a row in which we are editing text, insert another after it, and want to continue editing in the new row.

  • [row 0] cell_0 with textView that is firstresponder

Then we call insertRows(at: [.init(row: 1, section:0)], with: …). Within UITableViewDataSource.tableView(…, cellForRowAt:) we correctly get tableView do dequeue a new instance cell_1. (side note: all cells have same reuseIdentifier). In the same tableView(…, cellForRowAt:) method we configure the cell's textView to become the new firstResponder.

OptionSet vs Set

For a simple element type, pros & cons

Example UseCase

struct Model {
   let id: String
   let title: String
@hannesoid
hannesoid / InputAccessoryHostingController.swift
Last active November 19, 2024 16:33
Use a SwiftUI View as inputAccessoryView, dynamically adjusting to height changes
// (c) Hannes Oud @hannesoid
/// Hosts a SwiftUI view for use as an`inputAccessoryView`
///
/// - Implements a subclass of `UIInputViewController`, this allows setting it as `UITextView.inputAccessoryViewController`
/// - Has as a `.view` a `UIView` subclass that provides a `.intrinsicContentSize` which returns the height of a SwiftUI view
/// - Has a `UIHostingViewController` subclass as a child view controller. Invalidates the `.view`'s `intrinsicContentSize` when the SwiftUI view layouts, in order to inform the system to update the size of the `inputAccessoryView`
///
/// **Usage**
///