Skip to content

Instantly share code, notes, and snippets.

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

PreviewProvider in UIKit UIView

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

ViewProvider

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

ViewRepresentable.

ViewRepresentable is a struct complies with Protocol "UIViewRepresentable"

file: ViewPreviewProvider

You can cache the data that UIView 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 UIViewController

//
// 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
//
// ViewPreviewProvider.swift
//
// Created by Woody on 2022/2/10.
//
import Foundation
#if canImport(SwiftUI) && DEBUG
import SwiftUI
extension ViewProvider {
/// current preview
static var display: ViewProvider {
return .default
}
}
struct UIView_Preview: PreviewProvider {
static var previews: some View {
return ViewProvider.display
}
}
#endif
//
// ViewProvider.swift
//
// Created by Woody on 2022/2/11.
//
import Foundation
#if canImport(SwiftUI) && DEBUG
import SwiftUI
public struct ViewProvider: View {
public let view: AnyView
public var body: some View {
return view
}
init<Content>(_ view: UIView, @ViewBuilder content: (ViewRepresentable)-> Content) where Content: View {
let vr = ViewRepresentable(view: view)
self.view = AnyView(content(vr))
}
init(_ view: UIView) {
let vr = ViewRepresentable(view: view)
self.view = AnyView(vr)
}
init(_ view: UIView, size: ViewLayout.Size) {
let vr = ViewRepresentable(view: view)
self.view = AnyView(vr.frame(width: size.width, height: size.height, alignment: .center))
}
/// sample
public static let `default`: ViewProvider = .init(UIView(), content: { $0.overlay(Color.red)})
}
#endif
//
// ViewRepresentable.swift
//
// Created by Woody on 2022/2/11.
//
import Foundation
#if canImport(SwiftUI) && DEBUG
import SwiftUI
@frozen public struct ViewRepresentable: UIViewRepresentable {
let view: UIView
public func makeUIView(context: Context) -> some UIView {
return view
}
public func updateUIView(_ uiView: UIViewType, context: Context) {}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment