์ƒˆ์†Œ์‹

๐Ÿ“ฑ iOS/-- UIKit

(iOS) UIWindow ์— ๊ด€ํ•˜์—ฌ

  • -

๊ฐœ๋ฐœ์ž ๋ฌธ์„œ๋ฅผ ์ฝ์–ด๋ณด๋ฉด ๋‹ค์Œ์˜ ๋‚ด์šฉ์ด ์žˆ๋‹ค.

Declaration

“์•ฑ์˜ UI์™€ View์˜ ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฐ์ฒด์— ๋Œ€ํ•œ ๋ฐฐ๊ฒฝ์ด๋‹ค.”

์—ฌ๊ธฐ๊นŒ์ง€๋งŒ ์ฝ์—ˆ์„๋•Œ๋Š” ๊ฐ์ด ์•ˆ์˜จ๋‹ค.

์ž์„ธํ•˜๊ฒŒ ๋งํ•˜๋ฉด UIKit์˜ ๋™์ž‘ ๋ฐฉ์‹์€ View ํ˜ผ์ž์„œ๋งŒ ์ด๋ฃจ์งˆ์ˆ˜ ์—†๊ณ , ๋‹ค๋ฅธ ๊ฐ์ฒด๋“ค๊ณผ ํ•จ๊ป˜ ์ด๋ฃจ์ง„๋‹ค. ์ด๋•Œ UIWindow ๋„ ์ผ๋ถ€ ์ฐธ์—ฌํ•˜๊ฒŒ ๋œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ญ”๊ฐ€ UIWindow๊ฐ€ UIView๋ณด๋‹ค ์ƒ์œ„ํด๋ž˜์Šค์ผ๊ฑฐ๋ผ๋Š” ์ด๋ฏธ์ง€๊ฐ€ ์žˆ์—ˆ๋Š”๋ฐ ๋ฐ˜๋Œ€์˜€๋‹ค  UIWindow๋Š” UIView์˜ ํ•˜์œ„ ํด๋ž˜์Šค๋‹ค.

UIWindow์˜ ์‹œ๊ฐ์  ์—ญํ• 

UIWindow์˜ ์—ญํ• ์€ View๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” “์•ก์ž" ์—ญํ• ์ด๋‹ค.(๋‹ค๋ฅธ ๋ทฐ๋ฅผ ๋‹ด๋Š” ์ปจํ…Œ์ด๋„ˆ) ๋ฐ˜๋“œ์‹œ UIWindow๊ฐ€ ์žˆ์–ด์•ผ ํ™”๋ฉด์— View๋“ค์„ ๋””์Šคํ”Œ๋ ˆ์ด ํ•  ์ˆ˜ ์žˆ๋‹ค. window๊ฐ€ ์•ก์ž๋ผ๋ฉด, ๋‚ด๋ถ€์˜ ๊ทธ๋ฆผ์€ rootViewController ๊ฐ€ ๋œ๋‹ค.

(window์˜ rootViewController๋Š” NavigationController, TabBarController, UIViewController ๋“ฑ์ด ๋  ์ˆ˜ ์žˆ๋‹ค.)

ํ•˜์ง€๋งŒ UIWindow๋Š” ์ž์ฒด์ ์ธ ์‹œ๊ฐ์ ์ธ ์š”์†Œ๊ฐ€ ์—†๋‹ค. UIView๋ฅผ ์ƒ์†๋ฐ›์ง€๋งŒ (๋”ฐ๋ผ์„œ window.background = .red ๊ฐ™์€ ์ฝ”๋“œ๋„ ์ปดํŒŒ์ผ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ์•„๋ฌด๋Ÿฐ ๋ณ€ํ™”๊ฐ€ ์—†๋‹ค.)

๊ทธ๋ฆฌ๊ณ  ์ƒˆ๋กœ์šด ํ™”๋ฉด์„ ๋‚˜ํƒ€๋‚ธ๋‹ค๊ณ  ํ•ด์„œ ์ƒˆ๋กœ์šด window ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๋Š”๊ฒƒ์ด ์•„๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋Œ€๋ถ€๋ถ„์˜ ์•ฑ์—์„œ๋Š” window๊ฐ€ ๋‹จ ํ•œ๊ฐœ๋งŒ ์‚ฌ์šฉ๋œ๋‹ค. (์˜ˆ์™ธ์ธ ๊ฒฝ์šฐ๋Š” ๊ธ€ ์•„๋žซ๋ถ€๋ถ„์— ์ •๋ฆฌํ•ด๋†“์Œ)

UIWindow์˜ ๊ธฐ๋Šฅ์  ์—ญํ• 

UIWindow๋Š” View๋“ค๊ณผ ํ•จ๊ป˜ ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ณ , ์•ฑ ์ž‘๋™์— ๊ธฐ๋ณธ์ ์ธ ์ž‘์—…๋“ค์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

  • ๋‹ค๋ฅธ Window์— ๋Œ€ํ•œ Window์˜ ๊ฐ€์‹œ์„ฑ์— ์˜ํ–ฅ์„ ์ฃผ๋Š” Window์˜ z์ถ• ์ˆ˜์ค€์„ ์„ค์ •ํ•œ๋‹ค.
  • Window์„ ํ‘œ์‹œํ•˜๊ณ  ํ‚ค๋ณด๋“œ ์ด๋ฒคํŠธ์˜ ๋Œ€์ƒ์œผ๋กœ ๋งŒ๋“ ๋‹ค.
  • ์ขŒํ‘œ ๊ฐ’์„ Window์˜ ์ขŒํ‘œ๊ณ„๋กœ ๋ณ€ํ™˜ํ•˜๊ฑฐ๋‚˜ ๊ทธ ๋ฐ˜๋Œ€๋กœ ๋ณ€ํ™˜.
  • Window์˜ RootViewController ๋ณ€๊ฒฝ
  • Window๊ฐ€ ํ‘œ์‹œ๋˜๋Š” ํ™”๋ฉด์„ ๋ณ€๊ฒฝ.

UIWindow์˜ ํ™œ์šฉ

  • ์•ฑ ์ฝ˜ํ…์ธ ๋ฅผ ๋‚˜ํƒ€๋‚ด๊ธฐ ์œ„ํ•ด main window๋ฅผ ์ œ๊ณตํ•œ๋‹ค.
  • ์ถ”๊ฐ€์ ์ธ ์ฝ˜ํ…์ธ ๋ฅผ ๋‚˜ํƒ€๋‚ด๊ธฐ ์œ„ํ•ด ์ถ”๊ฐ€์ ์ธ window๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.(ํ•„์š”ํ•œ ๊ฒฝ์šฐ์—๋งŒ)

 ์ผ๋ฐ˜์ ์ธ ๊ฐœ๋ฐœ์‹œ์— ๋‹ค๋ฃจ๋Š” ์œˆ๋„์šฐ๋Š” windows[0] (์œˆ๋„์šฐ๋“ค์„ ๋‹ด๊ณ ์žˆ๋Š” ๋ฐฐ์—ด์ค‘ ์ฒซ๋ฒˆ์งธ์ด๋ฉฐ key window) ์ฃผ๋กœ ์‚ฌ์šฉํ•˜๊ฒŒ ๋œ๋‹ค. ํ•˜์ง€๋งŒ ๊ทธ ์ด์™ธ์—๋„ ์œˆ๋„์šฐ๊ฐ€ ์ƒ์„ฑ๋˜๋Š” ๊ฒฝ์šฐ๋“ค์ด ์žˆ๋Š”๋ฐ, ์˜ˆ๋ฅผ๋“ค์–ด ํ‚ค๋ณด๋“œ๋Š” ์˜ฌ๋ผ์˜ฌ๋•Œ ๋งˆ๋‹ค ์ƒˆ๋กœ์šด window๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ์˜ฌ๋ผ์˜จ๋‹ค. ๊ทธ๋ฆฌ๊ณ  status bar๋„ ํ•˜๋‚˜์˜ window์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  alert๋„ window๋ฅผ ๋งŒ๋“ค์–ด ์ƒ์„ฑํ•œ๋‹ค.

UIWindow ๊ด€๋ จ ๋ฉ”์„œ๋“œ

UIWindowDidBecomeVisibleNotification
UIWindowDidBecomeHiddenNotification
UIWindowDidBecomeKeyNotification
UIWindowDidResignKeyNotification

Key Window

์ขŒํ‘œ์™€ ๊ด€๋ จ๋œ ์ด๋ฒคํŠธ๋Š” window ๋กœ ์ „๋‹ฌ๋œ๋‹ค. ํ•˜์ง€๋งŒ ์ขŒํ‘œ๊ฐ€ ์—†๋Š” ์ด๋ฒคํŠธ๋Š” key window ๋กœ ์ „๋‹ฌ๋ฐ›๋Š”๋‹ค.

๋‹ค์‹œ ๋งํ•˜๋ฉด, Key Window๋Š” ํ‚ค๋ณด๋“œ, ํ„ฐ์น˜์™€ ๊ด€๋ จ๋˜์ง€ ์•Š์€ ์ด๋ฒคํŠธ(์ขŒํ‘œ์™€ ๊ด€๋ จ ์—†๋Š” ์ด๋ฒคํŠธ)๋ฅผ ์ˆ˜์‹ ํ•œ๋‹ค. ์˜ค์ง ํ•˜๋‚˜์˜ window๋งŒ key window๊ฐ€ ๋  ์ˆ˜ ์žˆ๋‹ค. 

window์˜ RootViewController๋ฅผ ๋ณ€๊ฒฝํ•˜์—ฌ ํ™”๋ฉด์ „ํ™˜ํ•˜๊ธฐ

UIApplication.shared ์‹ฑ๊ธ€ํ†ค ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด ์ ‘๊ทผํ•ด์•ผํ•œ๋‹ค.

// MARK: - UIWindow์˜ rootViewController๋ฅผ ๋ณ€๊ฒฝํ•˜์—ฌ ํ™”๋ฉด์ „ํ™˜
    func changeRootViewController(_ viewControllerToPresent: UIViewController) {
        if let window = UIApplication.shared.windows.first {
            window.rootViewController = viewControllerToPresent
            UIView.transition(with: window, duration: 0.5, options: .transitionCrossDissolve, animations: nil)
        }
    }

 

 

Window๋ฅผ ์—ฌ๋Ÿฌ๊ฐœ ์‚ฌ์šฉํ•ด์•ผํ•˜๋Š”๊ฒฝ์šฐ?

iOS13๋ถ€ํ„ฐ iPad์—์„œ multi window๋ฅผ ์ง€์›ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์ด๋ฅผ ์œ„ํ•ด์„œ Scene delegate๊ฐ€ ์ถ”๊ฐ€๋˜์—ˆ๋‹ค.

storyBoard๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ์˜ Window

iOS ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒˆ๋กญ๊ฒŒ ์ƒ์„ฑํ•˜๊ฒŒ ๋˜๋ฉด, ์ž๋™์œผ๋กœ main.storyBoard๋„ ํ•จ๊ป˜ ์ƒ์„ฑ๋œ๋‹ค. ์ด๋Ÿฌํ•œ ๊ฒฝ์šฐ์—๋Š” ์ƒ๋‹จ์˜ SceneDeleagte๋ฅผ ๊ฑด๋“ค์ง€ ์•Š์•„๋„ ๋œ๋‹ค. (์Šคํ† ๋ฆฌ ๋ณด๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ž๋™์œผ๋กœ window ํ”„๋กœํผํ‹ฐ๊ฐ€ ์ด๋‹ˆ์…œ๋ผ์ด์ฆˆ๋œ๋‹ค.)

ํ•˜์ง€๋งŒ, storyBoard์—†์ด UI๋ฅผ ์ฝ”๋“œ๋กœ๋งŒ ๊ตฌ์„ฑํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ์ง์ ‘ UIWindow ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์ฃผ์–ด์•ผํ•œ๋‹ค.

 

 

storyBoard๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ์˜ Window (์ฝ”๋“œ๋กœ UI ์ž‘์„ฑ)

  1. ์ดˆ๊ธฐ ์ƒ์„ฑ๋˜๋Š” Main.storyBoard ํŒŒ์ผ ์‚ญ์ œ
  2. info.plist์—์„œ Storyboard Name(key๊ฐ’) ์‚ญ์ œ
  3. iOS13+ SceneDelegate์—์„œ, ์ด์ „๋ฒ„์ „์€ AppDelegate์—์„œ window ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค๋ฉด ๋œ๋‹ค

makeKeyAndVisible - > keyWindow๋กœ ์ง€์ •, ๋ฐ Visibleํ•˜๋„๋ก...

Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.