1
1

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 1 year has passed since last update.

SwiftUIからUIViewControllerを呼び出す方法

Posted at

背景

既存のアプリのView周りをSwiftUIで作り直すとき、一部の画面では既存の画面をそのまま利用したい場合に、ViewControllerをSwiftUIから呼び出す方法です。

実装

ViewControllerの作成

HogeView.swift
/// UIViewControllerRepresentableプロトコルを準拠するViewControllerを作成する
struct LoginViewController: UIViewControllerRepresentable {
    // UIViewControllerを作成するメソッド
    func makeUIViewController(context: Context) -> UIViewController {
        // 指定のUIViewControllerを作成する
        let myViewController: UIViewController = LoginViewController()
        return myViewController
    }

    // UIViewControllerを更新するメソッド
    func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
        // UIViewControllerを更新したときの処理
    }
}

ViewControllerの呼び出し

struct LoginView: View {
    var body: some View {
        LoginViewController()
    }
}
1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?