Migrating to SwiftUI

Co-existing with old UIViews

  • Keep your new Views and models based on SwiftUI ready.
  • In your Main Storyboard, create a new Hosting View Controller using the ‘+’ on the top right corner
  • Create a custom UIHostingController class
Add Hosting View Controller
import SwiftUI
import UIKit

class MyHostingViewController: UIHostingController<MyNewUI> {
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder, rootView: MyNewUI() )
    }
}

In the above code, we are creating a custom HostingViewController using our new SwiftUI based View MyNewUI

  • Go back to the storyboard, and assign this custom class to the new Hosting View Controller created, using Identity Inspector
  • Move the story board entry pointer to this Hosting View Controller so that your App starts with your new SwiftUI view.

Once UIKit Views are Ready in SwiftUI

We can simply update Info.plist to remove storyboard and add new UIApplicationSceneManifest (This you can simply copy from a new a new example SwiftUI project).

    <key>UIApplicationSceneManifest</key>
    <dict>
        <key>UIApplicationSupportsMultipleScenes</key>
        <false/>
        <key>UISceneConfigurations</key>
        <dict>
            <key>UIWindowSceneSessionRoleApplication</key>
            <array>
                <dict>
                    <key>UISceneConfigurationName</key>
                    <string>Default Configuration</string>
                    <key>UISceneDelegateClassName</key>
                    <string>$(PRODUCT_MODULE_NAME).SceneDelegate</string>
                </dict>
            </array>
        </dict>
    </dict>

And remove the storyboard reference from info.plist

	<key>UIMainStoryboardFile</key>
	<string>Main</string>

No related posts.

Leave a Reply

Required fields are marked *.


This site uses Akismet to reduce spam. Learn how your comment data is processed.