I have tried to addSubview
a SwiftUI View to UIView. self.view.addSubview(contentView)
Error: Cannot convert value of type 'ContentView' to expected argument type 'UIView'
Kindly help me to implement this UI.
import UIKit
import SwiftUI
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
view.backgroundColor = UIColor.lightGray
let contentView = ContentView()
view.addSubview(contentView) // Error: Cannot convert value of type 'ContentView' to expected argument type 'UIView'
}
}
struct ContentView: View {
var body: some View {
Text("Hello world")
}
}
View
protocol != aUIView
– NestUIHostingController
. developer.apple.com/videos/play/wwdc2019/231 – MisapprehendUIHostingController is a
UIViewController` and you wish to useaddSubview
, you'll also need to learn how to use a child view controller (done inUIKit
) and part of that involves adding the view as a subview to your parent view. The hosting controllers (one for UIKit, AppKit, and WatchKit) are full controllers, and are easily used for a full screen SwiftUI view. Hope this helps. – Misapprehend