-
-
Save alexpaul/875d1c8ce45a5f536d0c81087285f4d8 to your computer and use it in GitHub Desktop.
// accessing the window and changing the root view controller property | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// getting access to the window object from SceneDelegate | |
guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene, | |
let sceneDelegate = windowScene.delegate as? SceneDelegate | |
else { | |
return | |
} | |
let viewcontroller = UIViewController() | |
viewcontroller.view.backgroundColor = .blue | |
sceneDelegate.window?.rootViewController = viewcontroller | |
} | |
// extension for resetting the window on a UIKit application | |
extension UIViewController { | |
private func resetWindow(with vc: UIViewController?) { | |
guard let sceneDelegate = UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate else { | |
fatalError("could not get scene delegate ") | |
} | |
sceneDelegate.window?.rootViewController = vc | |
} | |
func showViewController(with id: String) { | |
let vc = storyboard?.instantiateViewController(identifier: id) | |
resetWindow(with: vc) | |
} | |
} | |
// example case of resetting the window from a login view controller | |
// this will transition from a login vc to a navigation controller with the app logic | |
@IBAction func explore(_ sender: UIButton) { | |
showViewController(with: "BBQNavController") | |
} |
Thanks for your share. I have a thought, how could I do in the switch root viewController when I use SotryBoard?
A common case is: The pages ordered by StoryBoard, the first page is login vc, then will switch to tabbar vc, then, how to switch with segue in Storyboard.
Thank you, your code was so helpful both in understanding the way I need to call it also in my personal project.
Thanks for your share. I have a thought, how could I do in the switch root viewController when I use SotryBoard?
A common case is: The pages ordered by StoryBoard, the first page is login vc, then will switch to tabbar vc, then, how to switch with segue in Storyboard.
Hi, @tangzzz-fan I just updated the gist for your use case with examples hope it helps, let me know.
Thank you, your code was so helpful both in understanding the way I need to call it also in my personal project.
@HuseynG so glad it helped.
Thanks for your response and update!
Thanks for your response and update!
๐๐พ
thanks
Thank you ๐
How do you know that the first scene you are getting with connectedScenes.first?
is the scene that you want? If you are using scenes and have the key UIApplicationSupportsMultipleScenes
of the UIApplicationSceneManifest
in your Info.plist
set to true
, you can have a lot of scenes and each one does have its own instances of your App's views and view controllers.
UIApplication.shared.connectedScenes
.filter({ $0.activationState == .foregroundActive })
.compactMap({$0 as? UIWindowScene})
@alexpaul
Value of type 'SceneDelegate' has no member 'window'
Error with code code
// extension for resetting the window on a UIKit application
extension UIViewController {
private func resetWindow(with vc: UIViewController?) {
guard let sceneDelegate = UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate else {
fatalError("could not get scene delegate ")
}
sceneDelegate.window?.rootViewController = vc
}
func showViewController(with id: String) {
let vc = storyboard?.instantiateViewController(identifier: id)
resetWindow(with: vc)
}
}
error
@2020KamelAlaghbari, window
isn't in the UISceneDelegate prototype, but when I create a new project (iOS, Storyboard) using Xcode 13.4.1, the project includes a class called SceneDelegate, that conforms to UIWindowSceneDelegate, that includes a var window: UIWindow?.
Thank you, your code was so helpful both in understanding the way I need to call it also in my personal project.