jaesung-jung/hostingview
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.
HostingView
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.
StatefulHostingView
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.
Requirements
- iOS 16+
- macCatalyst 16+
- tvOS 16+
Install
Swift Package Manager
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")
]
)
]
)License
MIT license. See LICENSE for details.
Package Metadata
Repository: jaesung-jung/hostingview
Default branch: main
README: README.md