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

[iOS] SnapKit / Then ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ด์šฉํ•ด ์ฝ”๋“œ๋กœ UI ๊ตฌํ˜„ํ•˜๊ธฐ

by Danna 2021. 11. 11.
728x90
728x90

SnapKit

iOS ์—์„œ Autolayout ์„ ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•  ๋•Œ ์‰ฝ๊ณ  ํŽธ๋ฆฌํ•˜๊ฒŒ ๋„์™€์ฃผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

 

  • ์ž‘์„ฑํ•ด์•ผํ•˜๋Š” ์ฝ”๋“œ์˜ ๊ธธ์ด๋ฅผ ์ค„์—ฌ์ฃผ๊ณ , ๊ฐ€๋…์„ฑ์„ ๋†’์—ฌ์ฃผ๋Š” ์žฅ์ ์ด ์žˆ๋‹ค.
  • ๋ทฐ ๊ณ„์ธต์˜ depth ๊ฐ€ ๊นŠ์–ด์ง€๋ฉด ๋ณต์žกํ•ด์งˆ ์ˆ˜ ์žˆ๋‹ค.

 

pure autolayout ์œผ๋กœ Autolayout ์„ ์ ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

view.addSubview(label)
label.translatesAutoresizingMaskIntoConstraints = false
let leading = label.leadingAnchor.constraint(equalTo: view.leadingAnchor)
let trailing = label.trailingAnchor.constraint(equalTo: view.trailingAnchor)
let centerX = label.centerXAnchor.constraint(equalTo: view.centerXAnchor)
let centerY = label.centerYAnchor.constraint(equalTo: view.centerYAnchor)
NSLayoutConstraint.activate([leading, trailing, centerX, centerY])

 

SnapKit ์„ ์ด์šฉํ•ด Autolayout ์„ ์ ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

 

  • superview ์— ๋Œ€ํ•œ constraint ์ ์šฉ์„ ์‰ฝ๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • translatesAutoresizingMaskIntoConstraints = false ์„ค์ •์€ ๊ธฐ๋ณธ์œผ๋กœ ๋˜์–ด์žˆ์–ด, ๋ณ„๋„๋กœ ์ž‘์„ฑํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.
let label = UILabel()
view.addSubview(label)
label.snp.makeConstraints { make in
    make.width.height.centerX.centerY.equalToSuperview()
}โ€‹

SnapKit ๊ตฌ์„ฑ ์š”์†Œ - ์†์„ฑ, ๋ฉ”์†Œ๋“œ

view.snp.makeConstraints { make in 
    make.{properties}.equalTo(...).{methods}
}

 

โœ”๏ธŽ makeConstraints ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด view ์— ๋Œ€ํ•œ constraints ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

 

  • viewAnchor ์™€ targetAnchor ๋ฅผ ์—ฐ๊ฒฐํ•ด์ฃผ๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.
  • properties , methods ๋Š” ์—ฌ๋Ÿฌ ๊ฐœ๋ฅผ ์ค‘์ฒฉํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

โœ”๏ธŽ properties : constraints ๋ฅผ ์ ์šฉํ•  ๋Œ€์ƒ

 

  • left / right / top / bottom / leading / trailing / lastBaseline
  • ํฌ๊ธฐ์™€ ๊ด€๋ จ๋œ ์†์„ฑ : size / width / height
  • ๊ฐ€์šด๋ฐ ์ •๋ ฌ ์†์„ฑ : centerX / centerY

 

โœ”๏ธŽ  eqaulTo(...) : constraints ๋ฅผ ์ ์šฉํ•˜๊ธฐ ์œ„ํ•œ ๊ธฐ์ค€์ด ๋˜๋Š” ๋Œ€์ƒ์„ ์„ค์ •ํ•˜๋Š” ๋ฉ”์†Œ๋“œ

 

  • equalTo / greaterThanOrEqualTo / lessThanOrEqualTo
  • ์œ„์˜ ๋ฉ”์†Œ๋“œ๋“ค์— ๋Œ€ํ•ด eqaulToSuperView() ๋“ฑ Super view ์— ์ ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ์ œ๊ณตํ•œ๋‹ค.
  • make.left.equalTo(view) == make.left.equalTo(view.snp.left)

 

โœ”๏ธŽ  methods : constraints ์— ๋Œ€ํ•œ ์ถ”๊ฐ€์ ์ธ ์ •๋ณด๋ฅผ ์„ค์ •ํ•˜๋Š” ๋ฉ”์†Œ๋“œ

 

  • multipliedBy / dividedBy / offset / inset
  • > offset : view ์™€ constraints ๋ฅผ ์ ์šฉํ•œ view ์™€์˜ ๊ฐ„๊ฒฉ
  • > inset : view ๋‚ด๋ถ€์˜ ์—ฌ๋ฐฑ
  • priority : ์šฐ์„ ์ˆœ์œ„ - integer or .low, .medium, .high, .required.

SnapKit - update constraints

constraints ๋ฅผ ์ƒ์„ฑํ•œ ๋’ค์— ์—…๋ฐ์ดํŠธํ•  ํ•„์š”๊ฐ€ ์žˆ์„ ๋•Œ, ๊ฐ€๋Šฅํ•œ ๋ฐฉ๋ฒ•๋“ค

 

1 constraint ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ์ €์žฅํ•˜๊ณ , ํ•„์š”์‹œ ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ํ†ตํ•ด ๊ฐ’์„ ๋ณ€๊ฒฝํ•œ๋‹ค.

 

2 snp.updateConstraints ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด constraints ๋ฅผ ์žฌ์ •์˜ํ•œ๋‹ค.
- makeConstraints ์™€ ๋น„์Šทํ•˜๊ฒŒ ์ž‘์„ฑํ•˜์ง€๋งŒ, ์ƒ์ˆ˜ ๊ฐ’๋งŒ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

3 snp.remakeConstraints ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด constraints ๋ฅผ ์žฌ์ •์˜ํ•œ๋‹ค.
- makeConstraints ์™€ ๋น„์Šทํ•˜์ง€๋งŒ, ๊ธฐ์กด์˜ constraints ๋ฅผ ์‚ญ์ œํ•œ ํ›„ ์ƒˆ๋กœ ์„ค์ •ํ•œ๋‹ค.


Then

  • ์ฃผ๋กœ UIView ๊ธฐ๋ฐ˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์†์„ฑ ์„ค์ •์„ ์ข€ ๋” ํŽธํ•˜๊ฒŒ ์œ„ํ•ด ์‚ฌ์šฉ๋œ๋‹ค.
  • ์ฝ”๋“œ๋กœ View ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ธฐ์กด ๋ฐฉ๋ฒ•์— ๋น„ํ•ด ๋ฐ˜๋ณต๋˜๋Š” ์ฝ”๋“œ๊ฐ€ ์ ์–ด์ง„๋‹ค๋Š” ์žฅ์ ์ด ์žˆ๋‹ค.
// 1. UILabel ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑ๊ณผ ์†์„ฑ ์„ค์ •์„ ๋”ฐ๋กœ ์ž‘์„ฑํ•œ ์ฝ”๋“œ
let label = UILabel()
label.textAlignment = .center
label.text = "Hello, World!"

// 2. ํด๋กœ์ €๋ฅผ ํ†ตํ•ด UILabel ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ๊ณผ ์†์„ฑ์„ ์„ค์ •ํ•˜๋Š” ์ฝ”๋“œ
let label: UILabel = {
  let label = UILabel()
  label.textAlignment = .center
  label.text = "Hello, World!"
  return label
}()

// 3. UILabel ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•ด then + ํด๋กœ์ €๋ฅผ ํ†ตํ•ด ์†์„ฑ์„ ์„ค์ •ํ•˜๋Š” ์ฝ”๋“œ
let label = UILabel().then {
  $0.textAlignment = .center
  $0.text = "Hello, World!"
}

Then ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๋‹ค๋ฅธ ๋ฉ”์†Œ๋“œ

  • then() : ํด๋กœ์ €๋ฅผ ํ†ตํ•ด ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ๊ณผ ํ•จ๊ป˜ ์†์„ฑ๊ฐ’ ์„ค์ •์ด ๊ฐ€๋Šฅํ•˜๋„๋ก ํ•œ๋‹ค.
  • with() : ํด๋กœ์ €๋ฅผ ํ†ตํ•ด value type ์„ ๋ณต์‚ฌ์™€ ํ•จ๊ป˜ ์†์„ฑ๊ฐ’ ์„ค์ •์ด ๊ฐ€๋Šฅํ•˜๋„๋ก ํ•œ๋‹ค.
  • do() : ํด๋กœ์ €๋ฅผ ํ†ตํ•ด ์ธ์Šคํ„ด์Šค์˜ ๋ฉ”์†Œ๋“œ ๋™์ž‘์ด ์ˆ˜ํ–‰ ๊ฐ€๋Šฅํ•˜๋„๋ก ํ•œ๋‹ค.

์ฐธ๊ณ  ๋ฌธ์„œ

 

728x90
728x90

'๐ŸŽ iOS > WWDC' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[WWDC18] iOS Memory Deep Dive  (2) 2022.11.27
[WWDC19] Advances in Collection View Layout - Part 1  (0) 2022.10.17
[WWDC19] Data Flow Through SwiftUI - Part 2  (0) 2022.08.30
[WWDC19] Data Flow Through SwiftUI - Part 1  (0) 2022.08.30
[WWDC19] LLDB: Beyond "po"  (0) 2022.07.04