๋ค์ด์ด๋ฆฌ ์ฑ์ ๊ฐ๋ฐํ๋ฉด์ ํ์ ํ ์ดํ๋ก์ ํธ๋ฅด ํ ๋๋ ํฌ๊ฒ ๊ณ ๋ คํ์ง ์์๋ ๋๋ ํ๋ฉด์ ํ ๋ฐฉ์์ ์ฌ์ฉ์์ ํธ์๋ฅผ ์ค์ ์ผ๋ก ์๊ฐํ๋ฉฐ ๊ฐ๋ฐํ๋ค ๋ณด๋ ํธ๋ฆฌํจ์ ์ํด present๋ฐฉ์์ผ๋ก ํ๋ฉด์ ๋์ฐ๋ ๋ฐฉ์์ push ๋ฐฉ์์ ์ฌ์ฉํด์ผ ํ๋ ์ํฉ์ด ์๊ฒผ๋ค.
present์ push ๋ฐฉ์์ ๊ฒฐ๊ณผ๋ฌผ๋ง ๋ณด์๋ฉด ํฌ๊ฒ ์ฐจ์ด๋ ์์ง๋ง ๋ด๊ฐ ์ง์ ์ฝ๋๋ฅผ ์์ฑํ๋ฉฐ ๋๋ ์ฐจ์ด์ ์์๋
1. ์ผ์ชฝ์ผ๋ก ์ค์์ดํํ์ฌ ๋ทฐ๋ฅผ ์ ๊ฑฐํ๋ ๋ฐฉ์
2. ์๋ก์ด ๋ทฐ๊ฐ ๋ํ๋ ๋ ์ ๋๋ฉ์ด์ ๋ฐฉ์ (๊ฐ๋ก ๋ฐฉํฅ์ผ๋ก ๋ทฐ๊ฐ ๋ํ๋๊ฒ ํ๊ณ ์ถ์ผ๋ฉด push๋ก ํด์ผ๋ง ํ๋ ๊ฒ ๊ฐ๋ค.)
๋ ์์๋ ๊ฒ ๊ฐ์๋ฐ ํฌ๊ฒ๋ ์ด์ ๋..? ์ด๊ฒ ๊ทธ๋ ๊ฒ ํฐ ์ฐจ์ด์ธ๊ฐ ํ๋๋ฐ ์ฌ์ํ ๋ฐฉ์์์ ๋ญ๊ฐ ์ฑ์ด ํ์ ํด๋ณด์ด๋..? ๋๋์ด ๋๋ ๊ฒ ๊ฐ์๋ค.
์ฒ์์๋ ๋ด๊ฐ ํธํ๋ค๊ณ ์๊ฐํ๋ ๋ฐฉ์์ธ present๋ก ๋ทฐ๋ฅผ ๋์ ๋๋ฐ ์ฝ๋๋ฅผ ์ง๋ค๋ณด๋ push๋ก ๋์์ผ๋ง ํ๋ ์ํฉ์ด ์๊ฒผ๋ค. ๋๋ ์ฑ์ ์ฌ์ฉํ ๋ ์ค์์ดํ๋ก ๋ทฐ๋ฅผ ์ ๊ฑฐํ๋๋ฐ ์ด ๋ฐฉ์์ present๋ก๋ ์ฌ์ฉ์ด ๋ถ๊ฐํ ๊ฒ ๊ฐ์๋ค.
๊ทธ๋ฆฌ๊ณ ์ฑ์ด ์ต์ด์คํ์ด๋ ์๋๋์ ๋ฐ๋ผ ์๋ตํด๋ ๋๋ ๋ทฐ๊ฐ ์์๊ธฐ ๋๋ฌธ์ ์กฐ๊ฑด์ ๋ฐ๋ผ ๋ค๋ฅด๊ฒ ๋ทฐ๋ฅผ ๋์์ฃผ์ด์ผ ํ๋๋ฐ, ๋ฉ์ธ์์ ๊ธฐ์กด์ ์์๋๋ ๋ทฐ๋ฅผ ๋ชจ๋ ์ ๊ฑฐํ์ฌ ๋ฃจํธ๋ทฐ๋ฅผ ๋ค์ ์ง์ ํด์ฃผ์ด์ผ ํ๋ค.
๋ฌธ์ ๋ ์ธํฐ๋ท์ ์๋ ์ฝ๋, ์ ์ ๋ฐ์์ ์ฐธ๊ณ ํ๋ ์ฝ๋์์๋ ์ฌ๋ฐ๋ฅด๊ฒ ์์ฑํ๋๋ฐ ํ๋ฉด์ ํ์ด ๋์ํ์ง ์์๋ค.
์ํ์ฐฉ์ค ๋์ ์์๋ธ ๋ฐฉ์์ ์คํ ๋ฆฌ๋ณด๋ ๊ตฌ์ฑ์ ๋ฌธ์ ์๋ค.
๋จผ์ ๋ฃจํธ๋ทฐ๋ก ์ง์ ํ๋ ๋ฐฉ๋ฒ์ด๋ค.
1. ์ฒซ ๋ฒ์งธ ๋ฐฉ๋ฒ
extension UIWindow {
func replaceRootViewController(_ replacementController: UIViewController, animated: Bool, completion: (() -> Void)?) {
let snapshotImageView = UIImageView(image: self.snapshot())
self.addSubview(snapshotImageView)
let dismissCompletion = { () -> Void in // dismiss all modal view controllers
self.rootViewController = replacementController
self.bringSubviewToFront(snapshotImageView)
if animated {
UIView.animate(withDuration: 0.4, animations: { () -> Void in
snapshotImageView.alpha = 0
}, completion: { (success) -> Void in
snapshotImageView.removeFromSuperview()
completion?()
})
}
else {
snapshotImageView.removeFromSuperview()
completion?()
}
}
if self.rootViewController!.presentedViewController != nil {
self.rootViewController!.dismiss(animated: false, completion: dismissCompletion)
}
else {
dismissCompletion()
}
}
func snapshot() -> UIImage {
UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.main.scale)
drawHierarchy(in: bounds, afterScreenUpdates: true)
guard let result = UIGraphicsGetImageFromCurrentImageContext() else { return UIImage.init() }
UIGraphicsEndImageContext()
return result
}
}
let vc = self.storyboard?.instantiateViewController(withIdentifier: "MainVC")
UIApplication.shared.keyWindow?.replaceRootViewController(vc!, animated: true, completion: nil)
2. ๋ ๋ฒ์งธ ๋ฐฉ๋ฒ
let vc = self.storyboard?.instantiateViewController(withIdentifier: "main")
UIApplication.shared.windows.first?.rootViewController = vc
UIApplication.shared.windows.first?.makeKeyAndVisible()
๊ทธ๋ฆฌ๊ณ ์คํ ๋ฆฌ๋ณด๋๋ ๋ค์๊ณผ ๊ฐ์ด ๊ตฌ์ฑํ์๋ค.
๋๋ ๋๋ฒ์งธ ์ฝ๋๋ก ํด๊ฒฐํ์๋๋ฐ, ์ฌ๊ธฐ์ ์ค์ํ ์ ์ด withIdentifier์ ๋ค๋น๊ฒ์ด์ ์ปจํธ๋กค๋ฌ์ ์์ด๋๋ฅผ ์ง์ ํ๊ณ ์์ฑํด์ฃผ์ด์ผ ํ๋ค.
๊ทธ๋ฆฌ๊ณ ๋ฃจํธ๋ทฐ๋ก ์ง์ ํ๊ณ ์ถ์ ๋ถ๋ถ์ ๋ค๋น๊ฒ์ด์ ์ปจํธ๋กค๋ฌ๋ฅผ root๋ก ์ฐ๊ฒฐํด์ฃผ์ด์ผ ํ๋ค.
์๊ฐํด๋ณด๋ฉด ํฌ๊ฒ ์ด๋ ค์ด ๋ฌธ์ ๋ ์๋์๋๋ฐ ๋๋ฌด present๋ฐฉ์์ผ๋ก๋ง ์ง๋ค๋ณด๋๊น navigation๋ฐฉ์์ด ๋ฏ์ค์ด์ ๋ง์ด ํค๋งจ๊ฒ ๊ฐ๋ค. ์์ผ๋ก๋ ๋ ๋ฐฉ์ ๋ชจ๋ ์ฌ์ฉํ๋๋ก ํด์ผ๊ฒ๋ฐใ ใ
'iOS > Swift' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[iOS] ํ์ ์ฃผ์(Type Annotation)๊ณผ ํ์ ์ถ๋ก (Type Inference) (0) | 2022.12.27 |
---|---|
[iOS/Swift] CoreData ๊ณต๋ถํ๊ธฐ (0) | 2022.08.23 |
Reactive X ๊ณต๋ถ (0) | 2022.07.19 |
[iOS/Swift] TableView ๋ง๋ค๊ธฐ (0) | 2022.03.21 |
[iOS/Swift] ํ๋ฉด์์ ํค๋ณด๋๊ฐ ์ฌ๋ผ์ง๊ฒ ํ๋ ๋ฐฉ๋ฒ (0) | 2022.02.10 |