UIFoundation
This framework is currently under development, and the stability of any API is not guaranteed.
A Swift package providing foundational UI components and utilities on top of AppKit/UIKit. Primarily targets macOS (AppKit) with cross-platform support for iOS, tvOS, visionOS, and Mac Catalyst.
Requirements
- Swift 6.2+ (language mode: Swift 5)
- macOS 14+ / iOS 13+ / Mac Catalyst 13+ / tvOS 13+ / visionOS 1+
Installation
Swift Package Manager
dependencies: [
.package(url: "https://github.com/Mx-Iris/UIFoundation", from: "0.1.0")
]
Products
| Product | Description |
|---|
| UIFoundation | Umbrella library re-exporting all public sub-modules |
| UIFoundationToolbox | Standalone extensions and utilities (usable independently) |
| UIFoundationAppleInternal | Private API wrappers (App Store rejection risk) |
Using with AppKitPlus
If your app links AppKitPlus, enable the AppKitPlus trait:
.package(
url: "https://github.com/Mx-Iris/UIFoundation",
branch: "main",
traits: ["AppKitPlus"]
)
Both packages extend the same AppKit types, and with the trait on UIFoundation steps aside wherever AppKitPlus already provides a member — NSEdgeInsets's Equatable conformance, for one. With AppKitPlus linked but the trait off, each package ships its own copy and code that imports both fails to compile with an ambiguous ==. The trait also makes LayerBackedView and LayerBackedViewController build on AppKitPlus's layer-backed base classes, and LayerBackedView's corner radius, background colour, border and shadow then come from AppKitPlus's NSLayerBackedView rather than from this package's renderer — same names, see LayerBackground.md. The trait requires AppKitPlus 0.24.0 or later.
Usage
Xibless Base Classes
All views and controllers are created purely in code — no Xib or Storyboard required. Override setup() for initialization:
class MyView: XiblessView {
let titleLabel = NSTextField(labelWithString: "")
override func setup() {
addSubview(titleLabel)
}
}
Typed view controllers with generic contentView:
class MyViewController: XiblessViewController<MyView> {
override func viewDidLoad() {
super.viewDidLoad()
contentView.titleLabel.stringValue = "Hello"
}
}
LayerBackedView provides built-in support for corner radius, border, shadow, and background color — all with automatic redraw on property change:
class CardView: LayerBackedView {
override func setup() {
cornerRadius = 12
backgroundColor = .white
shadowRadius = 4
shadowOpacity = 0.15
borderWidth = 1
borderColor = .separatorColor
}
}
.box Namespace Extensions
All extensions on framework types use the .box namespace (via FrameworkToolbox) to avoid naming collisions:
let cell = tableView.box.makeView(ofClass: MyCell.self)
tableView.box.scrollRowToVisible(row, animated: true, scrollPosition: .centeredVertically)
if tableView.box.hasValidClickedRow { /* ... */ }
let labels = view.box.subviews(type: NSTextField.self, depth: .max)
view.box.sendToFront()
// Content starts at the top left even when the document view is not flipped (a stack view):
// the scroll view comes with a FlippedClipView.
let scrollView = NSScrollView.box.makeFlipped()
Closure-Based Target-Action
Convert target-action to closures on NSControl, NSMenuItem, NSGestureRecognizer, NSToolbarItem, NSColorPanel:
button.box.actionBlock = { sender in
print("Clicked: \(sender)")
}
let click = NSClickGestureRecognizer { recognizer in
print("Clicked at \(recognizer.location)")
}
menuItem.box.action { item in
print("Selected: \(item.title)")
}
Typed Notification Messages
The typed notification messages Apple declares for AppKit (macOS 27) and UIKit (iOS 26), backported to every OS this package supports: 137 AppKit and 71 UIKit messages, each in its type's Backport namespace, built on FrameworkToolbox's NotificationCenter.Backport:
import UIFoundation
import FoundationToolbox
let token = NotificationCenter.default.addObserver(of: window, for: .didBecomeKey) { message in
print(message.window.title) // message: NSWindow.Backport.DidBecomeKeyMessage
}
Each message reads and writes its notification exactly as Apple's does — the conversions were taken from Apple's binaries and are tested against Apple's own messages. When the deployment target reaches macOS 27 / iOS 26, delete Backport. and the code uses Apple's API.