๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŽ iOS/SwiftUI

[iOS/SwiftUI] View, Stack ๊ธฐ๋ณธ ์ •๋ฆฌ

by Danna 2022. 4. 29.
728x90
728x90

SwiftUI ๋Š” UIKit ๊ณผ ๋น„์Šทํ•˜๊ฒŒ iOS ์•ฑ์˜ UI ๋ฅผ ๊ทธ๋ฆฌ๊ธฐ ์œ„ํ•œ ํ”„๋ ˆ์ž„์›Œํฌ์ž…๋‹ˆ๋‹ค. iOS13 ๋ถ€ํ„ฐ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜์ง€๋งŒ iOS14 ์ด์ƒ๋ถ€ํ„ฐ ๊ฐ€๋Šฅํ•œ ๊ธฐ๋Šฅ๋“ค์ด ๋งŽ์•„, ๋ฏธ๋‹ˆ๋ฉˆ iOS14 ์—์„œ ์ฃผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฑธ๋กœ ์•Œ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค! ๋‹ค์Œ ์‚ฌ์ด๋“œํ”„๋กœ์ ํŠธ๋‚˜ ํด๋ก ์ฝ”๋”ฉ์€ SwiftUI ๋กœ ๊ผญ ํ•ด๋ด์•ผ๊ฒ ์–ด์š”!! ๊ทธ๋Ÿฌ๊ธฐ ์œ„ํ•œ ์Šคํ„ฐ๋”” ์‹œ์ž‘~! ๐Ÿ’ช

SwiftUI ์— ๋Œ€ํ•œ ํŠน์ง•

- ์„ ์–ธ์  ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•ด ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค์˜ ๊ธฐ๋Šฅ์„ ๋ช…์‹œํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

- ๋“œ๋ž˜๊ทธ ์•ค ๋“œ๋กญ์œผ๋กœ UI ๋ฅผ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๊ณ , ๋ฏธ๋ฆฌ๋ณด๊ธฐ๊ฐ€ ๊ฐ€๋Šฅํ•œ ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. (๊ณผ์—ฐ ์ž์ฃผ์“ธ์ง€ ์˜๋ฌธ์ด์ง€๋งŒ์š”)

- ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค์˜ ์†์„ฑ์€ ๋ฉ”์†Œ๋“œ ์ฒด์ด๋‹(Method Chaining) ๋ฌธ๋ฒ•์„ ์ด์šฉํ•ด ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

View ํ”„๋กœํ† ์ฝœ์˜ Instance method ๋ฅผ ๋ณด๋ฉด ๋ฐ˜ํ™˜๊ฐ’์ด View ์ธ๊ฒƒ์„ ํ™•์ธํ•˜์‹ค ์ˆ˜ ์žˆ์–ด์š”. ๋ฉ”์†Œ๋“œ๊ฐ€ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ด ์—ฌ๋Ÿฌ ๋ฉ”์†Œ๋“œ๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ์„ ์–ธํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค! (๋‚˜์ค‘์— ํ•œ๋ฒˆ ์ •๋ฆฌํ•ด๋ด์•ผ๊ฒ ์–ด์š”) ๊ด€๋ก€์ ์œผ๋กœ ๊ฐ์ฒด์˜ ์„ ์–ธ๊ณผ ์„ค์ •์€ ์—”ํ„ฐ๋กœ ๊ตฌ๋ถ„ํ•ด ์ž‘์„ฑํ•œ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

 

View Protocol

  • View ํ”„๋กœํ† ์ฝœ์€ ์•ฑ์˜ UI ๋ฅผ ํ‘œํ˜„ํ•˜๊ณ , view ๋ฅผ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•œ modifiers ๋ฅผ ์ œ๊ณตํ•˜๋Š” ํƒ€์ž…์ž…๋‹ˆ๋‹ค.
  • Stack, Button, Text, Image, List, Spacker ๋ชจ๋‘ View ํ”„๋กœํ† ์ฝœ์„ ์ฑ„ํƒํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
  • View ํ”„๋กœํ† ์ฝœ์„ ์ฑ„ํƒํ•ด ์ปค์Šคํ…€ํ•œ View ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์–ด์š”. computed property ์ธ body ๋Š” ๋ฐ˜๋“œ์‹œ ๊ตฌํ˜„ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. body ๋ฅผ ํ†ตํ•ด ์ปค์Šคํ…€ํ•œ ๋ทฐ์˜ ๋‚ด์šฉ์„ ์ฑ„์›Œ์ฃผ๋Š” ๊ฒƒ์ด์—์š”. body ๋‚ด๋ถ€์—๋Š” ์—ฌ๋Ÿฌ ๋ทฐ๋“ค์„ ์ถ”๊ฐ€ํ•ด์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

๐Ÿ‘‰ https://developer.apple.com/documentation/swiftui/view

 

๐Ÿ“š Building Layouts with Stack Views

SwiftUI ์—์„œ๋Š” View ๋ฅผ ๋ฐฐ์น˜ํ•˜๊ธฐ ์œ„ํ•ด์„œ Stack View ๋ฅผ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. HStack, VStack, ZStack ์„ธ๊ฐ€์ง€๊ฐ€ ์žˆ๋Š”๋ฐ XYZ ์ถ• ๊ธฐ๋ฐ˜์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋ผ์š”. Stack ๋‚ด๋ถ€์—๋Š” View ๋ฅผ ๋ฐฐ์น˜ํ•  ์ˆ˜ ์žˆ๊ณ , Stack ๋˜ํ•œ ๋ฐฐ์น˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! (Stack ๋„ View ํ”„๋กœํ† ์ฝœ์„ ์ฑ„ํƒํ•˜๊ธฐ ๋•Œ๋ฌธ)

  • HStack : Horizontal Stack, ๊ฐ€๋กœ๋กœ ํ•˜์œ„ ๋ทฐ๋“ค์„ ๋ฐฐ์น˜ํ•ฉ๋‹ˆ๋‹ค.
  • VStack : Vertical Stack, ์„ธ๋กœ๋กœ ํ•˜์œ„ ๋ทฐ๋“ค์„ ๋ฐฐ์น˜ํ•ฉ๋‹ˆ๋‹ค.
  • ZStack : Z ์ถ•์—์„œ ํ•˜์œ„ ๋ทฐ๋“ค์„ ์—ฐ๋‹ฌ์•„ ๋ฐฐ์น˜ํ•œ๋‹ค. ๋จผ์ € ์„ ์–ธ๋œ ๋ทฐ๋Š” ๊ฐ€์žฅ ํ•˜๋‹จ์— ๊น”๋ฆฌ๊ณ , ๊ทธ ์œ„๋กœ ๋ทฐ๋“ค์ด ๋ฐฐ์น˜๋ฉ๋‹ˆ๋‹ค. 

๊ณต์‹๋ฌธ์„œ ์ด๋ฏธ์ง€

๐Ÿ‘‰ https://developer.apple.com/documentation/swiftui/building-layouts-with-stack-views

 

Stack ์ƒ์„ฑ์ž

// Horizontal Stack
// VerticalAlignment - top, center, bottom, firstTextBaseline, lastTextBaseline
HStack(alignment: VerticalAlignment = .center, spacing: CGFloat? = nil, content: () -> Content)

// Vertical Stack
// HorizontalAlignment - leading, center, trailing
VStack(alignment: HorizontalAlignment = .center, spacing: CGFloat? = nil, content: () -> Content)

// Z-axis
// Alignment - topLeading, top, topTrailing, leading, center, trailing, bottomLeading, bottom, bottmTrailing
ZStack(alignment: Alignment = .center, content: () -> Content)
  • alignment ๋ฅผ ๊ฐ–๋Š” ๊ฑด ๋™์ผํ•˜์ง€๋งŒ, HStack ์€ ๊ฐ€๋กœ๋กœ ๋ฐฐ์น˜๋˜๋Š” ์ค‘ ์„ธ๋กœ์˜ ์ •๋ ฌ์„ ๊ธฐ์ค€์žก๊ณ , VStack ์€ ์„ธ๋กœ๋กœ ๋ฐฐ์น˜๋˜๋Š” ์ค‘ ๊ฐ€๋กœ์˜ ์ •๋ ฌ์„ ๊ธฐ์ค€ ์žก๊ณ , ZStack ์€ ๊ฐ€๋กœ ์„ธ๋กœ ๋‘˜ ๋‹ค ์ •๋ ฌ์„ ๊ธฐ์ค€์žก๋Š” ์ ์ด ๋‹ค๋ฆ…๋‹ˆ๋‹ค.
  • spacing ์˜ ๊ธฐ๋ณธ๊ฐ’์€ nil ์ด์ง€๋งŒ, 0์œผ๋กœ ์„ค์ •๋˜๋Š”๊ฒŒ ์•„๋‹Œ ์„œ๋ธŒ๋ทฐ๋“ค ์กฐํ•ฉ์— ๋”ฐ๋ฅธ ๊ธฐ๋ณธ ๊ฐ’์ด ์„ค์ •๋ฉ๋‹ˆ๋‹ค! ์˜๋„ํ•˜์ง€ ์•Š์€ ๋นˆ์นธ์ด ์žˆ๋‹ค๋ฉด spacing ์„ ์กฐ์ ˆํ•ด๋ณด์„ธ์š” ๐Ÿ‘€

 

View - Instance Method

Layout ์„ค์ •์— ๋Œ€ํ•œ ๋ฉ”์†Œ๋“œ, ์ ‘๊ทผ์„ฑ ์ œ๊ณต ๋ฐ Input ์ด๋‚˜ Event ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ์ œ๊ณตํ•˜๊ณ  ์žˆ์–ด์š”. ์•„์ง ๋งŽ์ด ์‚ฌ์šฉํ•ด๋ณด์ง„ ์•Š์•„์„œ, ๊ฐ•์˜๋“ค์œผ๋ฉด์„œ ๋‚˜์˜จ ๋ช‡๊ฐœ๋งŒ ์ •๋ฆฌํ•ด๋‘˜๊ฒŒ์š”! 

// ํŠน์ • ์‚ฌ์ด์ฆˆ๋กœ ํฌ๊ธฐ๋ฅผ ์ง€์ •ํ•˜๋Š” ๋ฉ”์†Œ๋“œ
// width, height ๊ฐ€ nil ์ธ ๊ฒฝ์šฐ๋Š” ๊ฐ€๋Šฅํ•œ ์ตœ๋Œ€ ์‚ฌ์ด์ฆˆ๋กœ ์ง€์ •๋œ๋‹ค.
func frame(width: CGFloat? = nil, height: CGFloat? = nil, alignment: Alignment = .center) -> some View

// safe area ๋ฐ–์œผ๋กœ ๋ทฐ๋ฅผ ๋ฐฐ์น˜ํ•˜๊ธฐ ์œ„ํ•œ ๋ฉ”์†Œ๋“œ
// ๋ทฐ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ safe area ๋‚ด๋ถ€์— ๋ฐฐ์น˜๋œ๋‹ค.
func ignoresSafeArea(_ regions: SafeAreaRegions = .all, edges: Edge.Set = .all) -> some View

// ๋ทฐ์˜ ์—ฃ์ง€ ์ „์ฒด์— ์ผ์ •ํ•œ ํŒจ๋”ฉ์„ ๋”ํ•˜๋Š” ๋ฉ”์†Œ๋“œ
func padding(_ length: CGFloat) -> some View

// ๋ทฐ์˜ ํŠน์ • ์—ฃ์ง€์— ์ผ์ •ํ•œ ํŒจ๋”ฉ์„ ๋”ํ•˜๋Š” ๋ฉ”์†Œ๋“œ
// length ๊ฐ€ nil ์ผ ๊ฒฝ์šฐ, ํ”Œ๋žซํผ์—์„œ ์ง€์ •ํ•œ ๊ธฐ๋ณธ ๊ฐ’์„ ์‚ฌ์šฉํ•œ๋‹ค.
func padding(_ edges: Edge.Set = .all, _ length: CGFloat? = nil) -> some View

// ๋ทฐ์˜ ํŠน์ • ์—ฃ์ง€๋งˆ๋‹ค ๋‹ค๋ฅธ ํŒจ๋”ฉ๊ฐ’์„ ๋”ํ•˜๋Š” ๋ฉ”์†Œ๋“œ
func padding(_ insets: EdgeInsets) -> some View

frame ๋ฉ”์†Œ๋“œ

View.frame ๋ฉ”์†Œ๋“œ๋Š” ํŠน์ • ์‚ฌ์ด์ฆˆ๋กœ ํฌ๊ธฐ๋ฅผ ์ง€์ •ํ•˜๋Š” ๋ฉ”์†Œ๋“œ์—์š”. 

width, height ๊ฐ€ nil ์ธ ๊ฒฝ์šฐ๋Š” ๊ฐ€๋Šฅํ•œ ์ตœ๋Œ€ ์‚ฌ์ด์ฆˆ๋กœ ์ง€์ •๋ฉ๋‹ˆ๋‹ค! ํŒŒ๋ž€์ƒ‰๊ณผ ์ฃผํ™ฉ์ƒ‰ ํƒ€์›์„ ๋ณด๋ฉด ๋น„๊ต๊ฐ€ ๋ ๊ฑฐ์—์š”.

ZStack ํ™œ์šฉ & ignoresSafeArea ๋ฉ”์†Œ๋“œ

View ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ safe area ๋‚ด๋ถ€์— ๋ฐฐ์น˜๋˜๊ธฐ ๋•Œ๋ฌธ์—, ๋…ธ์น˜๋‚˜ ํ•˜๋‹จ ๋ฐ”๋ฅผ ๋ฎ๋„๋ก ๋ฐฐ์น˜ํ•˜๊ณ ์‹ถ๋‹ค๋ฉด, ignoresSafeArea ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค! ์™ผ์ชฝ๊ณผ ์˜ค๋ฅธ์ชฝ์ด ๋‹ค๋ฅธ์ ์„ ๋ณด์‹ค ์ˆ˜ ์žˆ์„๊ฑฐ์—์š”.

๊ทธ๋ฆฌ๊ณ  ์•„๋ž˜ ์ฝ”๋“œ์—์„œ ์ƒ‰์ƒ์„ ์ž…ํžˆ๊ธฐ ์œ„ํ•ด์„œ๋Š” ZStack ์„ ์ด์šฉํ•˜๋Š” ์ ๋„ ์ฐธ๊ณ ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค! UIKit ์—์„œ addSubview ๋กœ ์‚ฌ์šฉ๋๋˜ ๊ฑธ ZStack ์„ ์‚ฌ์šฉํ•ด์„œ ์ข€๋” ๋ช…ํ™•ํ•˜๊ฒŒ ๋‚˜ํƒ€๋‚ผ์ˆ˜๊ฐ€ ์žˆ๋Š” ๊ฒƒ ๊ฐ™์•„์š” ๐Ÿ‘

 

View padding & background

๋ทฐ์— ๋Œ€ํ•ด padding, background ๋ฅผ ํ•จ๊ป˜ ์ ์šฉํ•  ๋•Œ๋Š” ์ˆœ์„œ์— ์œ ์˜ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค! ๋ฉ”์†Œ๋“œ๋ฅผ ์ ์šฉํ•˜๊ณ  ๋ฐ˜ํ™˜๋œ ๊ฐ์ฒด์— ๋‹ค์‹œ ๋ฉ”์†Œ๋“œ๋ฅผ ์ ์šฉํ•˜๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ์›ํ•˜๋Š” ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค์ง€ ์•Š์„์ˆ˜๋„ ์žˆ์–ด์š”. 

background > padding ๋Š” text ์— background ์„ค์ •๋œ ํ›„ padding ์ ์šฉ์ด๋ฏ€๋กœ padding ์˜์—ญ์—๋Š” ์ƒ‰์ƒ์ด ์—†๊ฒŒ ๋ฉ๋‹ˆ๋‹ค! padding ๊นŒ์ง€ ์ƒ‰์ƒ์„ ์ง€์ •ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด, padding > background ์ˆœ์„œ๋กœ ์„ค์ •ํ•ด์•ผํ•ด์š” ๐Ÿ˜ฎ

 


์ฐธ๊ณ  ๋ฌธ์„œ

 

728x90
728x90