0

I'm migrating an app from Storyboards and UIKit to SwiftUI. It's a big app, so I am doing it in stages. Specifically, I am embedding a few old UIKit assets within SwiftUI using UIViewControllerRepresentable.

I'm loading a UIViewController from the storyboard using

let storyboard = UIStoryboard(name: "MainStoryboard", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "MyViewController") as! MyViewController

This works fine except that the instantiated MyViewController does not contain the sub views that are present in the storyboard / interfacebuilder. Is there a way to instantiate the view controller with the sub views?

4
  • 1
    If there are subviews in a view controller in the storyboard, and if instantiating the view controller with an identifier from the storyboard doesn't contain them, then the view controller with that identifier in the storyboard is not the one you think it is. Just temporarily add print(vc.view.subviews) to your code to confirm that you are getting some other view controller that really doesn't have subviews. Commented Apr 5, 2024 at 18:09
  • 1
    Thanks for the suggestion that helped me narrow down the issue. The subview do show up in vc.view.subviews but are not shown on the screen. The problem is that they were not placed correctly because the constraints got messed up in the process. Commented Apr 5, 2024 at 19:32
  • And in addition the toolbar items and navigation items are ignores. See stackoverflow.com/questions/59295507/… Commented Apr 5, 2024 at 19:41
  • UIViewControllerRepresentable is tricky to get right and even some of Apple's examples have mistakes. Maybe you could share your code for that? Getting update and make correct are essential Commented Apr 6, 2024 at 13:05

2 Answers 2

0

I think I figured it out. There were two issues at play here:

  1. The sub views were not visible because the layout got messed up (my bad).
  2. The navigation items and toolbar items were also not showing up. But that was a completely different issue. See: UIViewControllerRepresentable: Navigation Title and Bar Button Items Ignored in NavigationView
Sign up to request clarification or add additional context in comments.

Comments

-1

Verify:

  1. Is the storyboard's name correct?
  2. Is the View Controller's identifier correct?
  3. Ensure that your Code/Class and UI/VC are linked.
  4. The module-related checkbox is checked (in the storyboard under the class property).

If any one of the four is not completed to perfection, your code will definitely crash.

However, if you are experiencing simply black VC UI, it is possible that you are accessing VC from the incorrect storyboard.

specially:
let storyboard = UIStoryboard(name: "Main", bundle: nil)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.