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 right from your SwiftUI views.
UIKitViews is a part of VDLayout library that provides a DSL syntaxis for UIKit views and view controllers.
- Straightforward implementation of UIKit components into SwiftUI environment.
- Supports environment variables by
UIView
/UIViewController
keypathes. HostingView
, an analogy ofUIHostingController
for UIView that supports updating by keypath.SelfSizingHostingController
-UIHostingController
that matches the root view size.- Provides
uiKitViewFixedSize()
,uiKitViewContentMode()
methods for dynamic self-sizing of the 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 properties
Note
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
is this example are not hardcoded methods; they are key path 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 by UIView
/UIViewController
keypathes:
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 uiLabelFont
You 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 slightly differ between iOS 16+ and previous versions, it's recommended to test on different iOS versions.
If you notice some 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 ofUIHostingController
forUIView
. It supports updating by keypath.
struct SomeView: View {
var text: String
// ...
}
// ...
let hosting = HostingView(SomeView())
hosting.text = "new text" // it will update the view
SelfSizingHostingController
: This is anUIHostingController
that 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 build
Add 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.