SwiftUI is Apple's modern UI framework. While SwiftUI has come a long way since it was first released, it still lacks the ability to fully control every UI element in great detail, which is why many developers still use UIKit or partially use SwiftUI.
Apple provides a way to integrate UIKit and SwiftUI, but using the UIHostingController
within the UIView
hierarchy can be a bit cumbersome.
In iOS 16, Apple introduced UIHostingConfiguration
as a new way to integrate SwiftUI with UICollectionViewCell and UITableViewCell. Inspired by this, I created HostingView
.
This package provides two ways to integrate SwiftUI: HostingView
and StatefulHostingView<State>
.
When using UIHostingConfiguration to create a CustomView (not a Cell) and applying Auto Layout, intrinsicContentSize may not be calculated correctly. This package properly calculates the content size and works seamlessly with Auto Layout.
The most powerful aspect is when using StatefulHostingView to create a CustomView or CustomControl. It allows you to easily leverage SwiftUI’s features and the benefits of declarative programming within UIKit. The demo project includes an implementation of UISwitch with little code using SwiftUI. The demo also demonstrates the bounce effect and On/Off animation.
More simple than UIHostingConfiguration or UIHostingController.
You can wrap a stateless SwiftUI view.
let gradientText = HostingView {
Text("Hosting View")
.font(.largeTitle)
.fontWeight(.black)
.foregroundStyle(
.linearGradient(
colors: [.cyan, .indigo, .pink, .orange, .yellow],
startPoint: .topLeading,
endPoint: .bottomTrailing
)
)
}
The intrinsicContentSize
is also correctly calculated, ensuring that the view behaves appropriately according to the size provided by the SwiftUI view.
You can wrap a stateful SwiftUI view, and SwiftUI will re-render the view whenever it detects a state change.
let statefulText = StatefulHostingView(state: 1) { state in // 1 is initial value
VStack {
Text("Stateful Hosting View")
.font(.headline)
.fontWeight(.black)
Text("State is \(state)")
.font(.subheadline)
.fontWeight(.medium)
.foregroundStyle(.secondary)
}
}
statefulText.state = 100 // When the state changes, SwiftUI re-renders the view.
For more use cases, please refer to the Demo project.
The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler.
import PackageDescription
let package = Package(
name: "YourProject",
dependencies: [
.package(url: "https://github.com/Jaesung-Jung/HostingView.git", .upToNextMajor(from: "1.0"))
],
targets: [
.target(
name: "YourProject",
dependencies: [
.product(name: "HostingView", package: "HostingView")
]
)
]
)
MIT license. See LICENSE for details.