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.
Features
Straightforward implementation of UIKit components into SwiftUI environment.
Supports environment variables by UIView/UIViewController keypathes.
HostingView, an analogy of UIHostingController 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.
Usage
Using UIKitViews is as simple as placing the UIView or UIViewController you want within the UIKitView closure:
Note: .text and font are not hardcoded methods, it’s a property chaining, so any properties of your UIKit views can be used as modifier methods with UIKitView.
UIKitView also supports environment variables by UIView/UIViewController keypathes:
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)
Self-sizing with uiKitViewFixedSize()
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)
⚠️ Warning: The behavior of this method 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.
uiKitViewContentMode(_:)
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.
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.
HostingView and SelfSizingHostingController
The repository contains two other key features:
HostingView: This is an analogy of UIHostingController for UIView. 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 an UIHostingController that matches the View size, allowing your views to automatically adjust to the size of their content.
UIKitViews
UIKitViews is a SwiftUI wrapper around
UIView
andUIViewController
. 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.
Features
UIView
/UIViewController
keypathes.HostingView
, an analogy ofUIHostingController
for UIView that supports updating by keypath.SelfSizingHostingController
-UIHostingController
that matches the root view size.uiKitViewFixedSize()
,uiKitViewContentMode()
methods for dynamic self-sizing of the UIKit views.Usage
Using UIKitViews is as simple as placing the
UIView
orUIViewController
you want within theUIKitView
closure:Note:
.text
andfont
are not hardcoded methods, it’s a property chaining, so any properties of your UIKit views can be used as modifier methods withUIKitView
.UIKitView also supports environment variables by
UIView
/UIViewController
keypathes:If you need to access the environment, you can do it like this:
You can also bind SwiftUI environments to
UIKitView
:Self-sizing with
uiKitViewFixedSize()
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:⚠️ Warning: The behavior of this method 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.uiKitViewContentMode(_:)
The
uiKitViewContentMode(_:)
method adjusts the content resizing behavior of a UIView when its size is not fixed. You pass aUIKitViewContentMode
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:
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.
HostingView
andSelfSizingHostingController
The repository contains two other key features:
HostingView
: This is an analogy ofUIHostingController
forUIView
. It supports updating by keypath.SelfSizingHostingController
: This is anUIHostingController
that matches the View size, allowing your views to automatically adjust to the size of their content.Installation
Create a
Package.swift
file.Add the following line to your Podfile:
and run
pod update
from the podfile directory first.Author
dankinsoid, voidilov@gmail.com
License
UIKitView is available under the MIT license. See the LICENSE file for more info.