A SwiftUI wrapper around UIView and UIViewController

UIKitViews

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.
  • SelfSizingHostingControllerUIHostingController that matches the root view size.

Usage

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

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

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.

Installation

  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.0.1")
  ],
  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.

Author

dankinsoid, voidilov@gmail.com

License

UIKitView is available under the MIT license. See the LICENSE file for more info.

GitHub

View Github