Skip to content

Latest commit

ย 

History

History
131 lines (97 loc) ยท 7.01 KB

UIWindow.md

File metadata and controls

131 lines (97 loc) ยท 7.01 KB

UIWindow



UIWindow๋ž€?

  • UIWindow๋Š” ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค์— ๋ฐฐ๊ฒฝ(backdrop)์„ ์ œ๊ณตํ•˜๊ณ , ์ค‘์š”ํ•œ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ ํ–‰๋™์„ ์ œ๊ณตํ•˜๋Š” ๊ฐ์ฒด์ด๋‹ค.
  • iOS์•ฑ์€ ๋ชจ๋“  View๋“ค์˜ ์ปจํ…Œ์ด๋„ˆ ์—ญํ• ์„ ํ•˜๋Š” UIWindow ์ธ์Šคํ„ด์Šค๋ฅผ ํ•˜๋‚˜ ๊ฐ€์ง€๋Š”๋ฐ, UIWindow๋Š” UIView์˜ ํ•˜์œ„ํด๋ž˜์Šค์ด๋ฏ€๋กœ Window ๊ทธ ์ฐจ์ œ๋กœ View๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • UIWindow๋Š” View๋ผ๋Š” ์‚ฌ์ง„์„ ๋ผ์›Œ๋„ฃ๋Š” ์•ก์ž๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค.


UIWindow์˜ ์—ญํ• 

  • ์•ฑ์ด ๋ณด์—ฌ์ฃผ๋Š” ์ฝ˜ํ…์ธ ๋“ค์„ ํฌํ•จํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.
  • ๋ทฐ๋ฅผ ํฌํ•จํ•œ ๋‹ค๋ฅธ ์•ฑ ๋‚ด ์˜ค๋ธŒ์ ํŠธ๋“ค์—๊ฒŒ ์ด๋ฒคํŠธ๋ฅผ ์ „๋‹ฌํ•ด์ฃผ๋Š” ํ•ต์‹ฌ์ ์ธ ์—ญํ• ์„ ํ•œ๋‹ค.
  • ์•ฑ์˜ ๋ทฐ ์ปจํŠธ๋กค๋Ÿฌ์™€ ํ™”๋ฉด ๋ฐฉํ–ฅ์ „ํ™˜(landscape & portrait)์„ ์œ ์šฉํ•˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด ํ˜‘์—…ํ•œ๋‹ค.
  • ์œˆ๋„์šฐ๋Š” ๋ทฐ ์ปจํŠธ๋กค๋Ÿฌ์™€ ํ•จ๊ป˜ ๋™์ž‘ํ•˜๋ฉด์„œ ์ด๋ฒคํŠธ๋ฅผ ํ•ธ๋“ค๋งํ•˜๊ณ  ์•ฑ์˜ ๋™์ž‘์— ํ•„์ˆ˜์ ์ธ ์ž‘์—…๋“ค์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.


UIWindow๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ

  • 1. ๋‹น์—ฐํžˆ.. ์•ฑ์˜ ์ฝ˜ํ…์ธ ๋ฅผ ํ™”๋ฉด์— ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•˜์—ฌ Main window๋ฅผ ์ œ๊ณตํ•ด์•ผํ•  ๋•Œ
  • 2. ์ถ”๊ฐ€์ ์ธ ์ฝ˜ํ…์ธ ๋ฅผ ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•ด ์ถ”๊ฐ€์ ์ธ window๋ฅผ ์ƒ์„ฑํ•ด์•ผ ํ•  ๋•Œ (external display๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ)

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-01-19 แ„‹แ…ฉแ„’แ…ฎ 5 41 22



KeyWinow ๊ด€๋ จํ•˜์—ฌ

KeyWindow๋ž€?
  • ์‹œ์Šคํ…œ์ด ์ƒ์„ฑํ•œ ์œˆ๋„์šฐ์—์„œ ์ƒ์„ฑ๋œ ์ด๋ฒคํŠธ๋ฅผ ๋ฐ›๋Š” ์œˆ๋„์šฐ
  • ํ‚ค ์œˆ๋„์šฐ๋Š” isKeyWindow๋ผ๋Š” ์†์„ฑ์„ ํ†ตํ•ด ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ํ•˜๋‚˜์˜ ์‹œ์ ์—๋Š” ์˜ค์ง ํ•œ ๊ฐœ์˜ ์œˆ๋„์šฐ๋งŒ์ด ํ‚ค ์œˆ๋„์šฐ๊ฐ€ ๋  ์ˆ˜ ์žˆ๋‹ค.
  • ์•ฑ์—์„œ External Display๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ ์œˆ๋„์šฐ๋Š” 1๊ฐœ ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ, ์•ฑ์˜ ๋ฉ”์ธ ์œˆ๋„์šฐ๊ฐ€ ํ‚ค ์œˆ๋„์šฐ์ด๋‹ค.

KeyWindow๊ฐ€ ์‚ฌ์šฉ๋  ๋•Œ
์˜ˆ๋ฅผ ๋“ค์–ด ํ‚ค๋ณด๋“œ ์ฐฝ์€ ์‹œ์Šคํ…œ์ด ์ƒ์„ฑํ•œ ์œˆ๋„์šฐ์ด๋‹ค.
ํ‚ค๋ณด๋“œ ์ฐฝ์—์„œ ๊ธ€์ž๋ฅผ ์ž…๋ ฅํ•  ๋•Œ ๋ฐœ์ƒํ•˜๋Š” ์ด๋ฒคํŠธ๋“ค์€ ์•ฑ์˜ ํ‚ค ์œˆ๋„์šฐ๋กœ ์ „๋‹ฌ์ด ๋œ๋‹ค.

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-01-19 แ„‹แ…ฉแ„’แ…ฎ 5 51 40



UIWindow ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒฝ์šฐ

๋ณดํ†ต Xcode๋Š” ํ”„๋กœ์ ํŠธ๊ฐ€ ์ƒ์„ฑ๋  ๊ฒฝ์šฐ ์ž๋™์œผ๋กœ AppDelegate์— window ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์—ฌ window ๊ฐ์ฒด๋ฅผ ๋˜ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜ ์ƒ์†๋ฐ›๋Š” ์ผ์€ ์ž˜ ์—†๋‹ค.
๋‹ค์Œ์€ window ๊ฐ์ฒด๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“ค๊ฑฐ๋‚˜ ๋งŒ๋“ค์–ด์ง€๋Š” ๊ฒฝ์šฐ๋‹ค.

  • 1. ์Šคํ† ๋ฆฌ๋ณด๋“œ๋ฅผ ์“ฐ์ง€ ์•Š์„ ๊ฒฝ์šฐ์—๋Š” ๋ฉ”์ธ window๋ฅผ ์ง์ ‘ ์ƒ์„ฑํ•ด์•ผ ํ•œ๋‹ค.
  • 2. ์™ธ๋ถ€ ๋””์Šคํ”Œ๋ ˆ์ด๋ฅผ ์ง€์›ํ•˜๋Š” ์•ฑ์˜ ๊ฒฝ์šฐ ์ƒˆ๋กœ์šด window๋ฅผ ๋งŒ๋“ค์–ด ๋ณด์—ฌ์ค„ ์ˆ˜ ์žˆ๋‹ค.
  • 3. ๋ณดํ†ต์˜ ๊ฒฝ์šฐ ์ƒˆ๋กœ์šด window๋Š” ์‹œ์Šคํ…œ์— ์˜ํ•ด ๋งŒ๋“ค์–ด์ง€๋ฉฐ ํŠน์ • ์ด๋ฒคํŠธ์— ๋Œ€ํ•œ ๋ฐ˜์‘์„ ๋งŒ๋“ค์–ด์ง„๋‹ค. ( ์˜ˆ: ์ „ํ™”๊ฐ€ ์˜ค๋Š” ๊ฒฝ์šฐ )


UIWindow ์ƒ์„ฑํ•˜๋Š” ๋ฒ•

๋ฉ”์ธ Window ๊ฐ์ฒด๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ / ์ธํ„ฐํŽ˜์ด์Šค ๋นŒ๋” ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์œผ๋กœ ์ƒ์„ฑ๊ณผ ์„ค์ •์„ ํ•  ์ˆ˜ ์žˆ๊ณ  AppDelegate ๊ฐ์ฒด๊ฐ€ ์ด๊ฒƒ์„ ์žก๊ณ  ์žˆ์–ด์•ผ ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ๊ฐ์ฒด๋Š” ์•ฑ ์ƒํƒœ์— ์ƒ๊ด€์—†์ด ์•ฑ์ด ์‹คํ–‰๋  ๋•Œ๋Š” ํ•ญ์ƒ ์ƒ์„ฑํ•ด์•ผ ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ถ”๊ฐ€์ ์ธ window ๊ฐ์ฒด๋Š” ๊ทธ๊ฒƒ์ด ํ•„์š”ํ•  ๋•Œ ์ƒ์„ฑ๋˜๋„๋ก(lazily created) ํ•ด์•ผ ํ•œ๋‹ค.

์Šคํ† ๋ฆฌ๋ณด๋“œ๋กœ๋Š” window๊ฐ€ ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋˜๋‹ˆ ์Šคํ† ๋ฆฌ๋ณด๋“œ ์—†์ด ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ ์œผ๋กœ ๋ฉ”์ธ Window ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๋ทฐ ์ปจํŠธ๋กค๋Ÿฌ์™€ ์—ฐ๊ฒฐํ•ด๋ณด๊ฒ ๋‹ค.

  • 1. Info.plist์— Main storyboard file base name์˜ Main์„ ์ง€์›Œ์ค€๋‹ค.
ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•  ๊ฒฝ์šฐ Xcode๋Š” ์ž๋™์œผ๋กœ Main.storyboard๋ฅผ ํ†ตํ•ด ๋ฉ”์ธ window ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ณ  rootViewController๋ฅผ ๊ฒฐ์ •ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์šฐ๋ฆฐ ์Šคํ† ๋ฆฌ๋ณด๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ง€์›Œ์ค€๋‹ค.

  • 2. window ๊ฐ์ฒด์™€ ๋ทฐ ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ์—ฐ๊ฒฐํ•œ๋‹ค.
AppDelegate์— ์˜ต์…”๋„ํ•œ window ๊ฐ์ฒด๊ฐ€ ์žˆ๋‹ค.
๋‹จ, SceneDelegate ๊ฐœ๋…์ด ๋“ฑ์žฅํ•œ ์ดํ›„์—๋Š” SceneDelegate ์˜ค๋ธŒ์ ํŠธ๊ฐ€ window ์†์„ฑ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.
class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?

}
์—ฐ๊ฒฐํ•˜๋Š” ์ž‘์—…์€ UIApplicationDelegate ๋ฉ”์†Œ๋“œ์ธ DidFinishLaunchingWithOptions์—์„œ ํ•ด์ค„ ๊ฒƒ์ด๋‹ค.
์•„๋ž˜์™€ ๊ฐ™์ด window๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ๋งŒ ํ•˜๊ณ  ์‹คํ–‰ํ•˜๋ฉด ์—๋Ÿฌ๊ฐ€ ๋‚œ๋‹ค.
window ๊ฐ์ฒด๋Š” ๋ทฐ๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ์ปจํ…Œ์ด๋„ˆ ์—ญํ• ์ด๊ธฐ ๋•Œ๋ฌธ์— rootViewController๋ฅผ ๋ฌด์กฐ๊ฑด ๊ฐ€์ ธ์•ผ ํ•œ๋‹ค.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        window = UIWindow(frame: UIScreen.main.bounds)
        ...

        return true
}
๊ทธ๋ž˜์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ์ƒ์„ฑํ•ด๋‘” UIViewController๋ฅผ ์œˆ๋„์šฐ์˜ rootViewController๋กœ ์ง€์ •ํ•ด์ฃผ๋ฉด ์ •์ƒ์ ์œผ๋กœ ์‹คํ–‰ํ•œ๋‹ค.
ํ•˜์ง€๋งŒ ๊ฒ€์€ ํ™”๋ฉด์ด ๋‚˜์˜จ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์ง€์ •ํ•œ window๊ฐ€ ๋ณด์ด์ง€ ์•Š๋Š”๋‹ค๋Š” ์˜๋ฏธ์ธ๋ฐ ์ด๊ฒƒ์€ ์šฐ๋ฆฌ๊ฐ€ window๊ฐ€ keyWindow๊ฐ€ ์•„๋‹ˆ๋ผ๋Š” ๋œป์ด๋‹ค. (.isKeyWindow๋กœ ํ™•์ธ ๊ฐ€๋Šฅํ•˜๋‹ค.)
์—ฌ๊ธฐ์„œ KeyWindow๋ž€ ํ‚ค๋ณด๋“œ ์ด๋ฒคํŠธ, ํ„ฐ์น˜์™€ ๊ด€๋ จ์—†๋Š” ์ด๋ฒคํŠธ๋ฅผ ๋ฐ›๋Š” window๋ฅผ ๋งํ•œ๋‹ค. ํ„ฐ๋ฆฌ ์ด๋ฒคํŠธ๋Š” ํ„ฐ์น˜๊ฐ€ ์ผ์–ด๋‚œ ๋ฐ”๋กœ ๊ทธ ์œˆ๋„์šฐ์—๊ฒŒ ์ „๋‹ฌ๋˜์ง€๋งŒ ์ขŒํ‘œ๊ฐ’์ด ์—†๋Š” ์ด๋ฒคํŠธ๋Š” ์•ฑ์˜ keyWindow์— ์ „๋‹ฌ์ด ๋œ๋‹ค. (ํ•˜์ง€๋งŒ ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ ๋ฉ”์ธ ์œˆ๋„์šฐ๊ฐ€ keyWindow์ผ ๊ฒƒ์ด๋‹ค.)
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        guard let window = window else { return true }
        window.backgroundColor = UIColor.cyan

        var MyViewController = MyViewController()
        MyViewController.backgroundColor = UIColor.red

        window.rootViewController = myViewController

        return true
}
์šฐ๋ฆฌ์˜ window๊ฐ์ฒด๊ฐ€ keyWindow๊ฐ€ ๋˜๊ณ  ๋ณด์ด๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ๋ฉด rootViewController์˜ backgroundcolor์ธ red๊ฐ€ ๋ณด์ด๊ฒŒ ๋  ๊ฒƒ์ด๋‹ค.


UIWindow๊ฐ€ ๊ฐ€์ง„ ๊ฒƒ

UIWindow๋Š” ํƒ€์ž… ํ”„๋กœํผํ‹ฐ๋กœ window๊ฐ€ keyWindow๊ฐ€ ๋˜๊ฑฐ๋‚˜ ๋˜์ง€ ์•Š๋Š”์ง€, ํ™”๋ฉด์— ๋ณด์ด๋Š”์ง€ ์•ˆ๋ณด์ด๋Š”์ง€, ๋˜๋Š” ํ‚ค๋ณด๋“œ ๊ด€๋ จ Notification์„ ๊ฐ–๊ณ  ์žˆ์–ด์„œ ๊ฐ์ง€ํ•˜๊ณ  ์‹ถ์„ ๊ฒฝ์šฐ ์˜ต์ €๋ฒ„๋ฅผ ์ถ”๊ฐ€ํ•ด ์ฝ์œผ๋ฉด ๋œ๋‹ค.
๊ทธ๋ฆฌ๊ณ  ์›ํ•˜๋Š” ์œˆ๋„์šฐ์˜ ์ขŒํ‘œ๊ณ„๋กœ ๋ฐ”๊ฟ”์ฃผ๋Š” convert ์ธ์Šคํ„ด์Šค ๋ฉ”์†Œ๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์–ด ์œˆ๋„์šฐ ๊ฐ„ ์ขŒํ‘œ ์ „ํ™˜์„ ํ•˜๊ณ ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.


์ฐธ๊ณ ์ž๋ฃŒ