|
import UIKit |
|
import SwiftUI |
|
import PlaygroundSupport |
|
|
|
struct ContentView: View { |
|
var body: some View { |
|
NavigationView { |
|
VStack { |
|
// UIKitのViewを使う。こいつの階層内でNavigationLinkしてみる |
|
MyUIKitContentsView() |
|
} |
|
.navigationTitle("Title") |
|
} |
|
} |
|
} |
|
|
|
struct MyUIKitContentsView: UIViewRepresentable { |
|
func updateUIView(_ uiView: UIView, context: Context) {} |
|
|
|
func makeUIView(context: Context) -> UIView { |
|
let myView = MyUIKitView<MyNavigationView>() |
|
myView.setView(MyNavigationView()) |
|
return myView |
|
} |
|
} |
|
|
|
class MyUIKitView<T: View>: UIView { |
|
private let hostingController = UIHostingController<T?>(rootView: nil) |
|
|
|
override init(frame: CGRect) { |
|
hostingController.view.backgroundColor = .clear |
|
hostingController.view.clipsToBounds = true |
|
hostingController.view.translatesAutoresizingMaskIntoConstraints = false |
|
|
|
super.init(frame: frame) |
|
} |
|
|
|
required init?(coder aDecoder: NSCoder) { |
|
super.init(coder: aDecoder) |
|
} |
|
|
|
func setView(_ view: T) { |
|
hostingController.rootView = view |
|
addSubview(hostingController.view) |
|
|
|
let constraints = [ |
|
hostingController.view.topAnchor.constraint( |
|
equalTo: self.topAnchor, |
|
constant: 0 |
|
), |
|
hostingController.view.leftAnchor.constraint( |
|
equalTo: self.leftAnchor, |
|
constant: 0 |
|
), |
|
hostingController.view.bottomAnchor.constraint( |
|
equalTo: self.bottomAnchor, |
|
constant: 0 |
|
), |
|
hostingController.view.rightAnchor.constraint( |
|
equalTo: self.rightAnchor, |
|
constant: 0 |
|
), |
|
] |
|
NSLayoutConstraint.activate(constraints) |
|
} |
|
} |
|
|
|
struct MyNavigationView: View { |
|
var body: some View { |
|
NavigationLink( |
|
destination: Text("destination"), |
|
label: { Text("myNavigation") } |
|
) |
|
} |
|
} |
|
|
|
PlaygroundPage.current.liveView = UIHostingController(rootView: ContentView()) |