---
title: Using SwiftUI View from UIKit
slug: using-swiftui-view-from-uikit
docTags: 
createdAt: 2021-03-12T19:22:30.000Z
---

# In Storyboard

`SwiftUI` views can be used inside `UIKit` apps by creating classes that conforms to `UIHostingController` with type of `SwiftUI` view we want use.

```swift
import SwiftUI

struct TestView: View {
    var body: some View {
        Text("Hello, World!")
    }
}

struct TestView_Previews: PreviewProvider {
    static var previews: some View {
        TestView()
    }
}

class TestViewHostingController: UIHostingController<TestView> {
    required init?(coder aDecoder: NSCoder) {
        super.init(rootView: TestView())
    }
}

```

1. Create a class that conforms to `UIHostingController` with type of `SwiftUI` view in this case type is `TestView`
2. Add `required init?(coder aDecoder: NSCoder)` that calls `super.init(rootView: TestView())`, which creates `UIHostingController` with `TestView` as `rootView`. 
3. In Storyboard add `UIHostingController` and set its class to the `TestViewHostingController`

# From code

1. Instantiate `UIHostingController` with the `SwiftUI` view as `rootView`
2. Present instantiated hosting controller like `UIViewController`

```swift
import SwiftUI

let hostingController = UIHostingController(rootView: SwiftUIView())
present(hostingController, animated)
// or navigationController?.pushViewController(hostingController, animated: true)

```

