Skip to content

Instantly share code, notes, and snippets.

@alexpaul
Last active June 21, 2024 05:06
Show Gist options
  • Save alexpaul/875d1c8ce45a5f536d0c81087285f4d8 to your computer and use it in GitHub Desktop.
Save alexpaul/875d1c8ce45a5f536d0c81087285f4d8 to your computer and use it in GitHub Desktop.
Getting access to the SceneDelegate window object in Xcode 11. SceneDelegate. Reset application window. Reset window. Reset rootviewcontroller. Root view controller. Scene delegate.
// 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")
}
@HuseynG
Copy link

HuseynG commented Aug 6, 2020

Thank you, your code was so helpful both in understanding the way I need to call it also in my personal project.

@tangzzz-fan
Copy link

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.

@alexpaul
Copy link
Author

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.

@alexpaul
Copy link
Author

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.

@tangzzz-fan
Copy link

Thanks for your response and update!

@alexpaul
Copy link
Author

Thanks for your response and update!

๐Ÿ‘๐Ÿพ

@TusharSharma651
Copy link

thanks

@nakulsinghsapient
Copy link

Thank you ๐Ÿ˜Š

@DG0BAB
Copy link

DG0BAB commented Aug 9, 2021

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.

Copy link

ghost commented Apr 28, 2022

@DG0BAB

UIApplication.shared.connectedScenes
            .filter({ $0.activationState == .foregroundActive })
            .compactMap({$0 as? UIWindowScene})

@AlaghbariKamel
Copy link

@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

@mr-fixit
Copy link

@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?.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment