Skip to content

Instantly share code, notes, and snippets.

@Kyle-Ye
Kyle-Ye / CAHostingLayer.md
Last active April 3, 2026 15:26
Using CAHostingLayer in SwiftUI — access SwiftUI's SPI CALayer without Apple internal SDK

Using CAHostingLayer in SwiftUI

Background

SwiftUI provides more than just UIHostingController / _UIHostingView (iOS) and NSHostingController / NSHostingView (macOS) for embedding SwiftUI views into UIKit/AppKit.

Starting from iOS 18.0 / macOS 15.0, SwiftUI also offers CAHostingLayer<Content: View> — a CALayer subclass that can host SwiftUI content directly at the layer level. This API is currently marked as SPI (@_spi(ForUIKitOnly) / @_spi(ForAppKitOnly)) and is used internally by UIKit, AppKit, and WebKit.

Accessing CAHostingLayer without Apple Internal SDK

@Kyle-Ye
Kyle-Ye / disable-calayersystem-swiftui-ios26.md
Created March 8, 2026 18:12
Disabling CALayerSystem Behavior in SwiftUI on iOS 26

Disabling CALayerSystem Behavior in SwiftUI on iOS 26

Starting with iOS 26, SwiftUI's UIHostingView may use CALayer-based rendering (caLayerSystem) instead of the traditional UIKit-based rendering (uikitSystem) when certain conditions are met. This can cause unexpected changes in the view hierarchy — for example, _UIGraphicsView and CGDrawingView subviews may be replaced by plain CALayer and CGDrawingLayer sublayers.

Here are two approaches to temporarily disable this behavior in your local debug environment.

Option 1: Environment Variable

Set the environment variable SWIFTUI_DISABLE_MIXED_VIEW_HIERARCHY=1 in your scheme's Run configuration (Edit Scheme → Run → Arguments → Environment Variables).

@Kyle-Ye
Kyle-Ye / ContentView.swift
Last active October 14, 2025 01:49
SwiftUI implicitRootType Playground
// Usage: ViewGraph.current.append(feature: HStackImplicitFeature())
struct HStackImplicitFeature: ViewGraphFeature {
func modifyViewInputs(inputs: inout _ViewInputs, graph: ViewGraph) {
inputs.implicitRootType = _HStackLayout.self
}
}
struct ContentView: View {
var body: some View {
ChildView1()
@Kyle-Ye
Kyle-Ye / kdebug_interpose.c
Created October 3, 2025 12:15
Make kdebug_is_enabled always return true
// kdebug_interpose.c
#include <stdbool.h>
#include <stdint.h>
#include <dlfcn.h>
// Forward declare the original
extern bool kdebug_is_enabled(uint32_t debugid);
// Our replacement
@Kyle-Ye
Kyle-Ye / DemoTests.swift
Created September 16, 2025 05:52
Unit Test SwiftUI redraws demo from OpenSwiftUI code
import Testing
import SwiftUI
@testable import Demo
typealias PlatformViewController = UIViewController
typealias PlatformWindow = UIWindow
typealias PlatformHostingController = UIHostingController
extension PlatformViewController {
// NOTE: Remember to withExtendedLifetime for window to ensure it is not deallocated duration animation or update.
@Kyle-Ye
Kyle-Ye / com.github.actions-runner.plist
Last active August 31, 2025 18:59
macOS Launch Agent for GitHub Actions Runner - Automatically runs ./run.sh in background at bootup with auto-restart and logging
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.github.actions-runner</string>
<key>ProgramArguments</key>
<array>
<string>/bin/bash</string>
@Kyle-Ye
Kyle-Ye / swift_6.1_linux_toolchain_patch.sh
Created May 11, 2025 09:36
A patch to workaround Swift 6.1 include CoreFoundation issue on Linux
#!/bin/bash
# A temporay workaround for https://github.com/swiftlang/swift-corelibs-foundation/issues/5211
set -e
# Find the path to swift binary
SWIFT_PATH=$(which swift)
echo "Swift binary found at: $SWIFT_PATH"
# Extract the toolchain path from swift binary path
# Remove /usr/bin/swift from the path to get the toolchain root
@Kyle-Ye
Kyle-Ye / SemanticsTestsApp.swift
Created March 12, 2025 03:28
_TestApp.setSemantics
// Ref: https://x.com/KyleSwifter/status/1899663558914572653
// The iOS 18 SwiftUI SDK has an API to change the semantic - _TestApp().setSemantics(xx).
// Remember to make it public in your SDK.
// For lower OS version, the API does not exist.
// But `_TestApp.run()` will read and set it from the `CommandLine.arguments` after `--semantics`.
@main
struct SemanticsTestsApp: App {
init() {
let app = _TestApp()
@Kyle-Ye
Kyle-Ye / build.sh
Last active October 21, 2024 08:58
Swift 5.10 Build script
// Set up swift repo
mkdir -p ~/tmp/build/swift-project
cd ~/tmp/build/swift-project
git clone git@github.com:swiftlang/swift.git
// Update the swift-project
cd swift
utils/update-checkout --clone-with-ssh
// Check out to the 5.10 release