63 lines
2.0 KiB
Swift
63 lines
2.0 KiB
Swift
//
|
|
// UIHostingView.swift
|
|
// PayCash
|
|
//
|
|
// Created by Saveliy Stavitsky on 2/1/22.
|
|
// Copyright © 2022 AM. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import SwiftUI
|
|
#if os(macOS)
|
|
public typealias PlatformViewType = NSView
|
|
#elseif !os(watchOS)
|
|
import UIKit
|
|
public typealias PlatformViewType = UIView
|
|
#endif
|
|
|
|
#if !os(watchOS)
|
|
@available(iOS 13.0, macOS 10.15, tvOS 13.0, *)
|
|
open class UIHostingView<Content> : PlatformViewType where Content : View {
|
|
#if os(macOS)
|
|
typealias HostingController = NSHostingController
|
|
#else
|
|
typealias HostingController = UIHostingController
|
|
#endif
|
|
private let hostingVC: HostingController<Content>
|
|
public var rootView: Content {
|
|
get { self.hostingVC.rootView }
|
|
set { self.hostingVC.rootView = newValue }
|
|
}
|
|
|
|
public init(rootView: Content,
|
|
frame: CGRect = .zero,
|
|
backgroundColor: UIColor = .clear,
|
|
autoresizingMask: Bool = true) {
|
|
self.hostingVC = HostingController(rootView: rootView)
|
|
|
|
super.init(frame: frame)
|
|
self.backgroundColor = backgroundColor
|
|
self.translatesAutoresizingMaskIntoConstraints = autoresizingMask
|
|
self.addSubview(hostingVC.view)
|
|
|
|
self.hostingVC.view.translatesAutoresizingMaskIntoConstraints = false
|
|
self.hostingVC.view.backgroundColor = backgroundColor
|
|
NSLayoutConstraint.activate([
|
|
self.hostingVC.view.topAnchor.constraint(equalTo: self.topAnchor),
|
|
self.hostingVC.view.bottomAnchor.constraint(equalTo: self.bottomAnchor),
|
|
self.hostingVC.view.leadingAnchor.constraint(equalTo: self.leadingAnchor),
|
|
self.hostingVC.view.trailingAnchor.constraint(equalTo: self.trailingAnchor)
|
|
])
|
|
}
|
|
|
|
@available(*, unavailable)
|
|
required public init?(coder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
var sizeThatFits: CGSize {
|
|
self.hostingVC.view.sizeThatFits(UIScreen.main.bounds.size)
|
|
}
|
|
}
|
|
#endif
|