I have seen a lot of other stackoverflow posts related to this error, but I'm not able to understand it (and thus, don't know how to solve) in my case.
Most of the responses in stackoverflow posts (like this one, for example) involve multiple view controllers and how you push one view controller into the stack before the previous one is finished.
But in my case, I only have a single view controller with very very very minimal UI - I got this error in a sample test code.
There's only one view controller in the project,
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
NSLog(TAG + "ViewController.viewDidLoad")
super.viewDidLoad()
view.backgroundColor = .systemBlue
}
}
The view controller is initialized in scene(willConnectTo:options) and the UI is displayed as shown below. But there is a custom class (Presentation) in between.
This is my scene(willConnectTo:options)
// uses Presentation class to display UI
func scene(_ pUIScene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
NSLog(TAG + "SceneDelegate.scene(willConnectTo:options)")
AppDelegate.sForegroundScene = pUIScene
var user_interface = Presentation()
user_interface.CreateWindow()
}
and this is my Presentation class,
class Presentation {
var window: UIWindow?
init() {
NSLog(TAG + "Presentation.init")
}
func CreateWindow() {
NSLog(TAG + "Presentation.CreateWindow")
guard let winScene = (AppDelegate.sForegroundScene as? UIWindowScene) else {
NSLog(TAG + "Error in obtaining UIWindowScene!")
return
}
window = UIWindow(windowScene: winScene)
window?.rootViewController = ViewController()
window?.makeKeyAndVisible()
}
}
Now, if I remove the Presentation class and directly initialise the ViewController and set UI in scene(willConnectTo:option) - as shown below, it works as expected - I get a blue screen.
// Presentation class is not used
func scene(_ pUIScene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
NSLog(TAG + "SceneDelegate.scene(willConnectTo:options)")
AppDelegate.sForegroundScene = pUIScene
guard let winScene = (AppDelegate.sForegroundScene as? UIWindowScene) else {
NSLog(TAG + "Error in obtaining UIWindowScene!")
return
}
window = UIWindow(windowScene: winScene)
window?.rootViewController = ViewController()
window?.makeKeyAndVisible()
}
Why does moving the UI code to a different class cause this 'Unbalanced Calls to begin/end appearance transitions' issue??