๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ“ฑ Mobile/iOS

iOS - UIKit vs SwiftUI ์— ๋Œ€ํ•ด์„œ (1)

SwiftUI์— ๋Œ€ํ•ด์„œ  → 2023.07.16 - [iOS] - UIKit vs SwiftUI ์— ๋Œ€ํ•ด์„œ - (1)

 

 

UIKit ์ด๋ž€?

iOS ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค(UI)๋ฅผ ๊ตฌ์ถ•ํ•˜๊ณ  ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ํ”„๋ ˆ์ž„์›Œํฌ์ด๋‹ค.

 

Apple์—์„œ ๊ฐœ๋ฐœํ•œ UIKit์€ iOS ๊ฐœ๋ฐœ์— ํ•ต์‹ฌ์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋ฉฐ, ์•ฑ์˜ ํ™”๋ฉด ๊ตฌ์„ฑ, ์‚ฌ์šฉ์ž ์ž…๋ ฅ ์ฒ˜๋ฆฌ, ์• ๋‹ˆ๋ฉ”์ด์…˜, ๊ทธ๋ฆฌ๊ณ  ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ ๋“ฑ์„ ๋‹ค๋ฃจ๋Š”๋ฐ ํ•„์ˆ˜์ ์ธ ๋„๊ตฌ๋“ค์„ ์ œ๊ณตํ•œ๋‹ค. 

 

UIKit์€ ViewController์™€ Storyboard๋ฅผ ์ค‘์‹ฌ์œผ๋กœ UI๋ฅผ ๊ฐœ๋ฐœํ•œ๋‹ค. ์ธํ„ฐํŽ˜์ด์Šค ๋นŒ๋”๋ฅผ ์‚ฌ์šฉํ•ด์„œ UI ์š”์†Œ๋ฅผ ์‹œ๊ฐ์ ์œผ๋กœ ๋ฐฐ์น˜ํ•˜๊ณ  ์ฝ”๋“œ๋กœ ์ด๋ฅผ ์กฐ์ž‘ํ•œ๋‹ค. 

 

 

UIKit์˜ ์ฃผ์š” ๊ธฐ๋Šฅ๊ณผ ๊ตฌ์„ฑ ์š”์†Œ (์˜ˆ์‹œ ์ฝ”๋“œ)

 

UIView 

UIKit์˜ ๊ธฐ๋ณธ์ ์ธ ๊ตฌ์„ฑ ์š”์†Œ์ด๋‹ค. ํ™”๋ฉด์— ๋ณด์ด๋Š” ๋ชจ๋“  UI ๊ฐ์ฒด๋“ค์˜ ๊ธฐ๋ณธ ํด๋ž˜์Šค์ด๋‹ค. 

๋ฒ„ํŠผ, ๋ ˆ์ด๋ธ”, ์ด๋ฏธ์ง€๋ทฐ ๋“ฑ ๋‹ค์–‘ํ•œ UI ์š”์†Œ๋“ค์€ UIView๋ฅผ ์ƒ์†ํ•˜์—ฌ ์ƒ์„ฑ๋œ๋‹ค. 

 

import UIKit

class MyView: UIView {
    override func draw(_ rect: CGRect) {
        // ๋ทฐ ๋‚ด๋ถ€์— ๊ทธ๋ฆฌ๊ธฐ
        let context = UIGraphicsGetCurrentContext()	//ํ˜„์žฌ ๊ทธ๋ž˜ํ”ฝ ์ปจํ…์ŠคํŠธ ์–ป์Œ
        context?.setFillColor(UIColor.blue.cgColor)	//๊ทธ๋ž˜ํ”ฝ ์ปจํ…์ŠคํŠธ์˜ ์ƒ‰์ƒ์„ blue๋กœ ์„ค์ •
        context?.fill(rect)	//์‚ฌ๊ฐํ˜•์„ blue๋กœ ์ฑ„์›€
    }
}

 

 

 

UIViewController

ํ™”๋ฉด์„ ๊ด€๋ฆฌํ•˜๊ณ  View์˜ ๋™์ž‘์„ ์ œ์–ดํ•˜๋Š” ํด๋ž˜์Šค์ด๋‹ค. 

ํ™”๋ฉด์„ ๋กœ๋“œํ•˜๊ณ  ํ•ด์ œํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋˜๋ฉฐ, ๋‹ค๋ฅธ ๋ทฐ ์ปจํŠธ๋กค๋Ÿฌ์™€์˜ ์ „ํ™˜์„ ๊ด€๋ฆฌํ•œ๋‹ค. 

 

import UIKit

class MyViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // ๋ทฐ ์ปจํŠธ๋กค๋Ÿฌ๊ฐ€ ๋กœ๋“œ๋  ๋•Œ ์‹คํ–‰๋  ์ดˆ๊ธฐํ™” ์ž‘์—…
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        // ๋ทฐ๊ฐ€ ํ™”๋ฉด์— ๋‚˜ํƒ€๋‚˜๊ธฐ ์ง์ „์— ์‹คํ–‰๋  ์ž‘์—…
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        // ๋ทฐ๊ฐ€ ํ™”๋ฉด์— ๋‚˜ํƒ€๋‚œ ํ›„์— ์‹คํ–‰๋  ์ž‘์—…
    }
}

 

 

 

UIWindow 

์•ฑ์˜ ๊ธฐ๋ณธ ์ฐฝ์œผ๋กœ, ํ™”๋ฉด์— ํ‘œ์‹œ๋˜๋Š” ๋ชจ๋“  ์ปจํ…์ธ ๋ฅผ ๊ด€๋ฆฌํ•œ๋‹ค. 

์ตœ์ƒ์œ„ ๋ทฐ์ด๋ฉฐ, ๋‹ค๋ฅธ ๋ชจ๋“  ๋ทฐ๋“ค์€ UIWindow ์œ„์— ํ‘œ์‹œ๋œ๋‹ค.

 

 

 

UIViewController์˜ View Life Cycle

UIViewController๋Š” ๋ทฐ์˜ ์ƒ๋ช… ์ฃผ๊ธฐ์— ๋”ฐ๋ผ ์—ฌ๋Ÿฌ ๋ฉ”์†Œ๋“œ๋“ค์„ ํ˜ธ์ถœํ•œ๋‹ค. viewDidLoad, viewWillAppear, viewDidAppear, viewWillDisappear, viewDidDisappear ๋“ฑ์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ๋ทฐ ์ปจํŠธ๋กค๋Ÿฌ์˜ ์ƒ๋ช… ์ฃผ๊ธฐ๋ฅผ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

 

 

UINavigationController 
ํ™”๋ฉด ์ „ํ™˜๊ณผ ๋‚ด๋น„๊ฒŒ์ด์…˜ ์Šคํƒ ๊ด€๋ฆฌ๋ฅผ ์ œ๊ณตํ•˜๋Š” ์ปจํŠธ๋กค๋Ÿฌ์ด๋‹ค. 

์Šคํƒ ํ˜•ํƒœ๋กœ ๋ทฐ ์ปจํŠธ๋กค๋Ÿฌ๋“ค์„ ๊ด€๋ฆฌํ•˜๋ฉฐ, ํ™”๋ฉด ์ „ํ™˜ ์‹œ ์• ๋‹ˆ๋ฉ”์ด์…˜๊ณผ ๋‚ด๋น„๊ฒŒ์ด์…˜ ๋ฐ”๋ฅผ ์ œ๊ณตํ•œ๋‹ค. 

 

import UIKit

class FirstViewController: UIViewController {
    // FirstViewController์˜ ์ฝ”๋“œ
}

class SecondViewController: UIViewController {
    // SecondViewController์˜ ์ฝ”๋“œ
}

// UINavigationController๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ™”๋ฉด ์ „ํ™˜ํ•˜๊ธฐ
let firstVC = FirstViewController()
let navController = UINavigationController(rootViewController: firstVC)
navController.pushViewController(SecondViewController(), animated: true)

 

 

 

UITabBarController

ํƒญ ํ˜•ํƒœ๋กœ ํ™”๋ฉด์„ ์ „ํ™˜ํ•˜๊ณ  ์—ฌ๋Ÿฌ ํƒญ๋“ค ๊ฐ„์˜ ์ปจํ…์ธ ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ์ปจํŠธ๋กค๋Ÿฌ์ด๋‹ค. 

ํƒญ ๋ฐ”๋ฅผ ํ†ตํ•ด ์—ฌ๋Ÿฌ ๋ทฐ ์ปจํŠธ๋กค๋Ÿฌ๋“ค ๊ฐ„์— ์‰ฝ๊ฒŒ ์ „ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

import UIKit

class FirstViewController: UIViewController {
    // FirstViewController์˜ ์ฝ”๋“œ
}

class SecondViewController: UIViewController {
    // SecondViewController์˜ ์ฝ”๋“œ
}

// UITabBarController๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํƒญ ํ™”๋ฉด ์ „ํ™˜ํ•˜๊ธฐ
let tabBarController = UITabBarController()
let firstVC = FirstViewController()
let secondVC = SecondViewController()
tabBarController.viewControllers = [firstVC, secondVC]

 

 

 

UIButton, UILabel, UIImageView 

UIButton์€ ๋ฒ„ํŠผ, UILabel์€ ํ…์ŠคํŠธ๋ฅผ ํ‘œ์‹œํ•˜๋Š” ๋ ˆ์ด๋ธ”, UIImageView๋Š” ์ด๋ฏธ์ง€๋ฅผ ํ‘œ์‹œํ•˜๋Š” ๋ทฐ ๋“ฑ, ๋‹ค์–‘ํ•œ UI ์š”์†Œ๋“ค์ด UIKit์— ๊ตฌํ˜„๋˜์–ด ์žˆ๋‹ค. 

 

import UIKit

class MyViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // ๋ฒ„ํŠผ 
        let button = UIButton(type: .system)
        button.frame = CGRect(x: 100, y: 100, width: 200, height: 50)
        button.setTitle("Click Me!", for: .normal)
        button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
        self.view.addSubview(button)
        
        // ๋ ˆ์ด๋ธ” 
        let label = UILabel(frame: CGRect(x: 100, y: 200, width: 200, height: 50))
        label.text = "Hello, World!"
        self.view.addSubview(label)
        
        // ์ด๋ฏธ์ง€๋ทฐ 
        let imageView = UIImageView(frame: CGRect(x: 100, y: 300, width: 200, height: 150))
        imageView.image = UIImage(named: "myImage")
        self.view.addSubview(imageView)
    }
    
    @objc func buttonTapped() {
        print("Button tapped")
    }
}

 

 

 

์• ๋‹ˆ๋ฉ”์ด์…˜๊ณผ ๊ทธ๋ž˜ํ”ฝ ์ฒ˜๋ฆฌ

UIKit์€ UIView์˜ ์• ๋‹ˆ๋ฉ”์ด์…˜ ๊ธฐ๋Šฅ๊ณผ ๊ทธ๋ž˜ํ”ฝ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ๋‹ค์–‘ํ•œ ํด๋ž˜์Šค๋“ค์„ ์ œ๊ณตํ•œ๋‹ค.

UIView์˜ ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ํ™”๋ฉด ์ „ํ™˜๊ณผ UI ์š”์†Œ์˜ ์›€์ง์ž„์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค

 

 

 

UIKit์˜ ์žฅ์ 

- ์‚ฌ์šฉ์ด ๊ฐ„ํŽธํ•˜๊ณ  ์ง๊ด€์ ์ด๋‹ค.

iOS ๊ฐœ๋ฐœ์ž๋“ค์ด ์‰ฝ๊ฒŒ UI๋ฅผ ๊ตฌ์ถ•ํ•˜๊ณ  ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค๊ณ„๋˜์—ˆ๋‹ค. ๊ฐ„๋‹จํ•œ ์ฝ”๋“œ๋กœ ๋‹ค์–‘ํ•œ UI ์š”์†Œ๋“ค์„ ์ƒ์„ฑํ•˜๊ณ  ๋ฐฐ์น˜ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, Interface Builder๋ฅผ ํ†ตํ•ด ์‹œ๊ฐ์ ์œผ๋กœ UI๋ฅผ ๋””์ž์ธํ•  ์ˆ˜๋„ ์žˆ๋‹ค. 

 

- ๋„ค์ดํ‹ฐ๋ธŒ ์„ฑ๋Šฅ

iOS ์šด์˜์ฒด์ œ์— ์ตœ์ ํ™”๋˜์–ด ์žˆ์–ด์„œ ๋„ค์ดํ‹ฐ๋ธŒ ์•ฑ๊ณผ ๋™์ผํ•œ ์„ฑ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค. ๋•Œ๋ฌธ์—, ์•ฑ์ด ๋น ๋ฅด๊ณ  ๋ฐ˜์‘์„ฑ์ด ์ข‹์œผ๋ฉฐ ์‚ฌ์šฉ์ž ๊ฒฝํ—˜์„ ํ–ฅ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.

 

- ๋‹ค์–‘ํ•œ UI ์š”์†Œ๊ณผ ์ปค์Šคํ„ฐ๋งˆ์ด์ง•

๋‹ค์–‘ํ•œ UI ์š”์†Œ๋“ค์„ ์ œ๊ณตํ•˜๋ฉฐ, ์ด๋ฅผ ์กฐํ•ฉํ•˜๊ณ  ์ปค์Šคํ„ฐ๋งˆ์ด์ง•ํ•˜์—ฌ ์›ํ•˜๋Š” ๋””์ž์ธ๊ณผ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

- ์• ๋‹ˆ๋ฉ”์ด์…˜๊ณผ ๊ทธ๋ž˜ํ”ฝ ์ฒ˜๋ฆฌ 

UIView์˜ ์• ๋‹ˆ๋ฉ”์ด์…˜ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ถ€๋“œ๋Ÿฌ์šด ํ™”๋ฉด ์ „ํ™˜๊ณผ UI ์š”์†Œ์˜ ์›€์ง์ž„์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

- ์•ˆ์ •์„ฑ๊ณผ ๋ณด์•ˆ 

Apple์—์„œ ๊ฐœ๋ฐœํ•˜๊ณ  ์œ ์ง€๋ณด์ˆ˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์•ˆ์ •์„ฑ๊ณผ ๋ณด์•ˆ์— ์‹ ๋ขฐ์„ฑ์ด ์žˆ๋‹ค. 

728x90