Is there any way to use storyboard and SwiftUI in same iOS Xcode project?

后端 未结 5 1249
有刺的猬
有刺的猬 2020-12-13 06:58

As Swift 5 introduces the SwiftUI framework for creating the views, but we are currently using the storyboard for UI design.

So I just wanted to know the procedure t

5条回答
  •  攒了一身酷
    2020-12-13 07:12

    I just started to look at the SwiftUI. Sharing a small example.

    1. In the storyboard add Hosting View Controller
    2. Subclass the UIHostingController with your own class (ChildHostingController)
    3. ChildHostingController should look something like that:
    
    import UIKit
    import SwiftUI
    
    struct SecondView: View {
      var body: some View {
          VStack {
              Text("Second View").font(.system(size: 36))
              Text("Loaded by SecondView").font(.system(size: 14))
          }
      }
    }
    
    class ChildHostingController: UIHostingController {
    
        required init?(coder: NSCoder) {
            super.init(coder: coder,rootView: SecondView());
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
        }
    }
    
    

    For more details have a look at Custom UIHostingController
    Apple Docs UIhostingController (Unfortunatelly it hasn't been documented yet)
    Integrating SwiftUI Video

提交回复
热议问题