Learn Courses My Dashboard

How can I replace a UIViewController with a SwiftUI View?

I am trying to incorporate some SwiftUI into my UIKit based iPhone application. I have a view controller in my Storyboard called ‘SellingNewsViewController’ with an associated “.swift” file. In this “SellingNewsViewController.swift" file, I am using the following code to try and create a UIHostingController to show the SwiftUI code I currently have in another file called “ContentView.swift”.

   class SellingNewsViewController: UIViewController {
    override func viewDidLoad() {
let controller = UIHostingController(rootView: ContentView())
        controller.didMove(toParent: self)

And I have a SwiftUI file in this directory (the code I am trying to run) named ‘ContentView.swift’ which has the following Syntax:

import SwiftUI
import SwiftyJSON
import SDWebImageSwiftUI
import WebKit
struct ContentView: View {
    @ObservedObject var list = getData()
    var body: some View {
            List(list.datas){i in
                webView(url: i.url)
                                .navigationBarTitle("", displayMode: .inline)){
                    HStack(spacing: 15){
                       VStack(alignment: .leading, spacing: 10){
                        if i.image != "" {
                            WebImage(url: URL(string: i.image)!, options: .highPriority, context: nil)
                                .frame(width: 110, height: 135)
                    }.padding(.vertical, 15)
struct ChildHostingController_Previews: PreviewProvider {
    static var previews: some View {
struct dataType : Identifiable {
    var id : String
    var title : String
    var desc : String
    var url : String
    var image : String
class getData : ObservableObject{
    @Published var datas = [dataType]()
    init() {
        let source = "https://newsapi.org/v2/top-headlines?country=gb&category=business&apiKey=<mykeyishere>"
        let url = URL(string: source)!
        let session = URLSession(configuration: .default)
        session.dataTask(with: url) { (data, _, err) in
            if err != nil{
      let json = try! JSON(data: data!)
            for i in json["articles"] {
                let title = i.1["title"].stringValue
                let description = i.1["description"].stringValue
                let url = i.1["url"].stringValue
                let image = i.1["urlToImage"].stringValue
                let id = i.1["publishedAt"].stringValue
                DispatchQueue.main.async {
                    self.datas.append(dataType(id: id, title: title, desc: description, url: url, image: image))
struct webView : UIViewRepresentable {
 var url : String
    func makeUIView(context: UIViewRepresentableContext<webView>) -> WKWebView{
        let view = WKWebView()
        view.load(URLRequest(url: URL(string: url)!))
        return view
    func updateUIView(_ uiView: WKWebView, context: UIViewRepresentableContext<webView>) {

I understand that I need to use a UIHostingController to show SwiftUI views in my UIKit application, however whenever I load up my ‘SellingNewsViewController’, I just receive a blank screen, as shown in the attached image.

Am I doing something wrong to try and create/show the UIHostingController?

Check out this link! Getting started with UIKit in SwiftUI and vice versa - SwiftLee

I’d recommend not making a storyboard and view controller file. And just do it all in code. There’s really not a need to have the storyboard when your view code is going to be in SwiftUI

You’re also making the SwiftUI view a child view of your VC, I believe you still need to set parameters for the view, and that’s why it’s not showing up

don’t really recommend this though, unless you made the “storyboard” app because its more natural for you

if you use SwiftUI the ios version of the user of your phone should be ios 13 and above, thus it will eliminate any old iphone models below those like iphone 6 or first generation ipad air

its better to do it all SwiftUI or you can do storyboard/programmatic UI instead