24
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Tips]SwiftUI側でViewControllerを表示

Last updated at Posted at 2019-09-04

このはじめに

SwiftUIからViewControllerを表示する時のTipsです。

ViewControllerとStoryboard

記事用に作成したViewControllerとStoryboardになります。

ViewController

SampleViewController.swift
import UIKit
import Instantiate
import InstantiateStandard

class SampleViewController: UIViewController, StoryboardInstantiatable {

    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

Storyboard

スクリーンショット 2019-09-04 19.18.03.png

UIViewControllerRepresentableを使用

SampleViewController.swift
import UIKit
import SwiftUI
import Instantiate
import InstantiateStandard

class SampleViewController: UIViewController, StoryboardInstantiatable {

    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

struct SampleViewControllerWrapper : UIViewControllerRepresentable {

    typealias UIViewControllerType = SampleViewController

    func makeUIViewController(context: UIViewControllerRepresentableContext<SampleViewControllerWrapper>) -> SampleViewControllerWrapper.UIViewControllerType {
        return UIViewControllerType.instantiate()
    }

    func updateUIViewController(_ uiViewController: SampleViewControllerWrapper.UIViewControllerType, context: UIViewControllerRepresentableContext<SampleViewControllerWrapper>) {
    }
}

SwiftUIからSampleViewControllerを表示する

SampleView.swift
import SwiftUI

struct SampleView: View {
    var body: some View {
        SampleViewControllerWrapper()
    }
}

#if DEBUG
struct SampleView_Previews: PreviewProvider {
    static var previews: some View {
        SampleView()
    }
}
#endif
スクリーンショット 2019-09-04 19.28.39.png
24
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
24
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?