UIKitViews is a SwiftUI wrapper around UIView and UIViewController. It provides seamless integration of UIKit components with the SwiftUI framework. The UIKitView wrapper makes it incredibly easy to add and manipulate UIKit views and view controllers directly from your SwiftUI views.
UIKitViews is built on top of VDChain and is part of the VDLayout library that provides DSL syntax for UIKit views and view controllers.
- Straightforward integration of UIKit components into SwiftUI environment
- Support for environment variables through
UIView/UIViewControllerkeypaths HostingView, an analog ofUIHostingControllerfor UIView that supports updating via keypathSelfSizingHostingController- aUIHostingControllerthat matches the root view size- Provides
uiKitViewFixedSize()anduiKitViewContentMode()methods for dynamic self-sizing of UIKit views
Using UIKitViews is as simple as placing the UIView or UIViewController you want within the UIKitView closure:
UIKitView {
UILabel().chain
.font(.systemFont(ofSize: 24)) // Constant properties
.textColor(.black)
}
.text(title) // Updatable propertiesNote
The UIKitView body closure is called only once when the view is created, so there is no reason to use any updatable variables in this closure. However, it’s the perfect place to set up constant parameters, such as constraints or fonts, for example.
Note
.text, .textColor, and .font in this example are not hardcoded methods; they are keypath chains. This means any properties of your UIKit views can be used as modifier methods with UIKitView.
UIKitViews provides a special operator § that allows you to create a UIKitView more concisely with an autoclosure:
UILabel()§
.font(.systemFont(ofSize: 24))
.textColor(.black)
.text(title)UIKitView also supports environment variables through UIView/UIViewController keypaths:
VStack {
UIKitView {
UILabel()
}
UIKitView {
UILabel()
}
}
.uiKitViewEnvironment(\UILabel.font, .systemFont(ofSize: 24))If you need to access the environment, you can do it like this:
@Environment(\UILabel.font) var uiLabelFontYou can also bind SwiftUI environments to UIKitView:
UIKitView {
UIScrollView()
}
.uiKitViewBind(environment: \.isScrollEnabled, to: \UIScrollView.isScrollEnabled)The library includes a method uiKitViewFixedSize() that allows the UIKit view to adjust its size dynamically according to its content. You can specify the axis for self-sizing:
- For self-sizing in both dimensions:
.uiKitViewFixedSize()- For self-sizing mostly in the vertical dimension:
.uiKitViewFixedSize(.vertical)- For self-sizing mostly in the horizontal dimension:
.uiKitViewFixedSize(.horizontal)Note
If you know the height or width of your view, it’s more reliable to set it using the SwiftUI frame modifier instead of uiKitViewFixedSize.
Warning
The behavior of these methods may differ slightly between iOS 16+ and previous versions. It's recommended to test on different iOS versions.
If you notice any undesirable differences, you can use the uiKitViewUseWrapper(.always) method to fix it.
The uiKitViewContentMode(_:) method adjusts the content resizing behavior of a UIView when its size is not fixed.
You pass a UIKitViewContentMode value to this method to specify how you want the view to resize its content.
It comes with two modes:
.fill: The content should resize to completely fill the view. The aspect ratio may not be preserved..fit(Alignment): The UIView should resize to fit within the view while preserving its aspect ratio. The alignment parameter determines how the UIView is positioned within the view if there is extra space.
Here's an example:
UIKitView {
UILabel().chain
.font(.system(34))
.textColor(.black)
.textAlignment(.left)
}
.uiKitViewFixedSize(.vertical)
.uiKitViewContentMode(.fit(.trailing))In this example, the UILabel will resize its content to fit within its bounds while preserving its aspect ratio. The content is positioned at the trailing edge of the UIKitView.
The repository contains two other key features:
HostingView: This is an analogy ofUIHostingControllerforUIView. It supports updating by keypath.
struct SomeView: View {
var text: String
// ...
}
// ...
let hosting = HostingView(SomeView())
hosting.text = "new text" // it will update the viewSelfSizingHostingController: This is anUIHostingControllerthat matches the View size, allowing your views to automatically adjust to the size of their content.
Create a Package.swift file.
// swift-tools-version:5.7
import PackageDescription
let package = Package(
name: "SomeProject",
dependencies: [
.package(url: "https://github.com/dankinsoid/UIKitViews.git", from: "1.5.0")
],
targets: [
.target(name: "SomeProject", dependencies: ["UIKitView"])
]
)$ swift buildAdd the following line to your Podfile:
pod 'UIKitViews'and run pod update from the podfile directory first.
dankinsoid, voidilov@gmail.com
UIKitView is available under the MIT license. See the LICENSE file for more info.