LoginSignup
2
3

More than 3 years have passed since last update.

SwiftUIからStoryboardを表示させる その2

Posted at

Storyboardを直接呼ぶ方法

利用方法は ?

  • 新規でプロジェクト名はお好みで?
  • User Interface: SwiftUIにして作成してください

新規プロジェクトが作成できたら?

Storyboardを作成してください
作成したStoryboardのViewの背景色とかを変えるとわかりやすいかも?

  • 「Main.storyboard」と命名して作成します
  • 「Main.storyboard」のCustom ClassのClassは「Viewcontroller」
  • 「Main.storyboard」のIdentityのStoryboard IDは「idMain」

Storyboardの設定が終わったら?

  • ContentView.swiftを入れ替える

ContentView.swift

ContentView.swift

//
//  ContentView.swift
//  SwiftUIView
//
//  Created by 福田 敏一 on 2019/12/24.
//  Copyright © 2019 株式会社パパスサン. All rights reserved.
//

import SwiftUI

struct ContentView: UIViewControllerRepresentable {

    typealias UIViewControllerType = ViewController

    func makeUIViewController(context: Context) -> ViewController {

        let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let nextView = storyboard.instantiateViewController(withIdentifier: "idMain") as! ViewController
        nextView.modalPresentationStyle = .fullScreen
print("1-20・通過・makeUIViewController・")
          return nextView
        }

    func updateUIViewController(_ uiViewController : ViewController, context: Context) {
print("3-25・通過・updateUIViewController・")
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
print("2-40・通過・ViewController")
    }

}

// 実行結果

1-20通過makeUIViewController
2-40通過ViewController
3-25通過updateUIViewController

私の感想と意見ですが ?

SwiftUIの使い方が? だんだんと? 理解が深まりました
なんとか「struct ContentView」からStoryboardを呼べないか? 試行錯誤してここまで辿り着きました? しかし、変な挙動なんですよね? 何がって? Storyboardで作成したMain.storybardのViewの背景色を変えると背景色は上部の時計とかがあるエリアまで指定した色になるのですが? SwiftUIで作成してMain.storyboardを追加した場合、Viewの背景色を赤にしても上部の時計の部分が白のままなんですよね? nextView.modalPresentationStyle = .fullScreenでは解決しないのでしょうか? これではまだまだ、理解が深まっていませんね? すぐに解決してみます、諦めない、諦めません?

ここまで

2
3
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
2
3