Skip to content

Instantly share code, notes, and snippets.

@TuenTuenna
Last active April 14, 2023 13:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TuenTuenna/4a12d3dee4ef98976817f7ff2215d113 to your computer and use it in GitHub Desktop.
Save TuenTuenna/4a12d3dee4ef98976817f7ff2215d113 to your computer and use it in GitHub Desktop.
ChangeRoot

UIkit 에서 루트 바꾸기

// UIApplication 익스텐션
extension UIApplication {
    
    func changeRoot(_ vc: UIViewController){
       // 애플리케이션 에서 키윈도우로 첫번째 윈도우 씬을 가져오고
        let scenes = UIApplication.shared.connectedScenes
        let windowScenes = scenes.first as? UIWindowScene
        
        let firstWindow = windowScenes?.windows.filter { $0.isKeyWindow }.first
       // 적용할 뷰컨을 루트뷰컨으로 변경 후
        firstWindow?.rootViewController = vc
       // 보여지게 설정합니다
        firstWindow?.makeKeyAndVisible()
    }
}

사용 예시

let vc = MainVC()

// 아무곳에서나 루트를 변경 할 수 있습니다
UIApplication.shared.changeRoot(vc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment