UIKitViews is a SwiftUI wrapper around UIView and UIViewController


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 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.


Using UIKitViews is as simple as placing the UIView or UIViewController you want within the UIKitView closure:

UIKitView {
       .font(.systemFont(ofSize: 24)) // Constant properties
.text(title) // Updatable properties

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:

VStack {
  UIKitView {
  UIKitView {
.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 {
.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:
  • For self-sizing mostly in the vertical dimension:
  • For self-sizing mostly in the horizontal dimension:

⚠️ 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.


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 {

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.


  1. Swift Package Manager

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
  1. CocoaPods

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.


  • Swift Tools 5.7.0
View More Packages from this Author


Last updated: Mon Mar 18 2024 21:30:53 GMT-0900 (Hawaii-Aleutian Daylight Time)