Skip to content

Instantly share code, notes, and snippets.

@woodycatliu
Last active February 14, 2022 08:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save woodycatliu/dddf0fdcc4a3c620f4fe65b0b062b126 to your computer and use it in GitHub Desktop.
Save woodycatliu/dddf0fdcc4a3c620f4fe65b0b062b126 to your computer and use it in GitHub Desktop.

PreviewProvider in UIKit UIViewController

We can preview the drawing of coding via SwiftUI.

This part will demonstrate the code of encapsulating SWiftUI PreviewProvider and how it can be used in UIKit.UIViewController.

ViewControllerProvider.

The init function is encapsulated with ViewControllerProvider the view and @ViewBuilder Or specified Size.
The type of ViewControllerProvider.viewController is AnyView, which means it will act like a previewer for Preview Provider.

ViewControllerRepresentable

ViewControllerRepresentable is a struct complies with Protocol "UIViewControllerRepresentable"

file: ViewControllerPreviewProvider.swift

You can cache the data that UIViewController holds and use them when you need to preview your code.

If you are interesting to know more about relative information, please check out PreviewProvider in UIKit UIView

//
// KitProviderLayout.swift
//
// Created by Woody on 2022/2/11.
//
import UIKit
#if canImport(SwiftUI) && DEBUG
class ViewLayout {
typealias Size = CGSize
}
extension ViewLayout.Size {
typealias Length = CGFloat
static func Square(_ length: Length)-> Self {
return Self.init(width: length, height: length)
}
static func Rectangle(width: Length, heightMultiplier m: Length)-> Self {
return Self.init(width: width, height: width * m)
}
}
extension ViewLayout.Size.Length {
static var ScreenWidth: Self {
return UIScreen.main.bounds.width
}
static var ScreenHeight: Self {
return UIScreen.main.bounds.height
}
}
#endif
//
// ViewControllerPreviewProvider.swift
//
// Created by Woody on 2022/2/10.
//
import Foundation
#if canImport(SwiftUI) && DEBUG
import SwiftUI
extension ViewControllerProvider {
static var display: ViewControllerProvider {
return .empty
}
}
struct ViewController_Preview: PreviewProvider {
static var previews: some View {
return ViewControllerProvider.display
}
}
#endif
//
// ViewControllerProvider.swift
//
// Created by Woody on 2022/2/11.
//
import Foundation
#if canImport(SwiftUI) && DEBUG
import SwiftUI
public struct ViewControllerProvider: View {
public let viewController: AnyView
public var body: some View {
return viewController
}
init<Content>(_ viewController: UIViewController, @ViewBuilder content: (ViewControllerRepresentable)-> Content) where Content: View {
let vr = ViewControllerRepresentable(vc: viewController)
self.viewController = AnyView(content(vr))
}
init(_ viewController: UIViewController) {
let vr = ViewControllerRepresentable(vc: viewController)
self.viewController = AnyView(vr)
}
init(_ viewController: UIViewController, size: ViewLayout.Size) {
let vr = ViewControllerRepresentable(vc: viewController)
self.viewController = AnyView(vr.frame(width: size.width, height: size.height, alignment: .center))
}
}
extension ViewControllerProvider {
public static var empty: ViewControllerProvider {
return ViewControllerProvider.init(UIViewController())
}
}
#endif
//
// ViewControllerRepresentable.swift
//
// Created by Woody on 2022/2/11.
//
import Foundation
#if canImport(SwiftUI) && DEBUG
import SwiftUI
@frozen public struct ViewControllerRepresentable: UIViewControllerRepresentable {
let vc: UIViewController
public func makeUIViewController(context: Context) -> some UIViewController {
return vc
}
public func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment