πWWDC23 | Category : UI Frameworks
πΒ https://developer.apple.com/videos/play/wwdc2023/10057/
π Discover powerful enhancements to the trait system in UIKit. Learn how you can define custom traits to add your own data to UITraitCollection, modify the data propagated to view controllers and views with trait override APIs, and adopt APIs to improve flexibility and performance. We'll also show you how to bridge UIKit traits with SwiftUI environment keys to seamlessly access data from both UIKit and SwiftUI components in your app.
Traits are independent pieces of data that the system automatically propagates to every view controller and view in your app
UIKit provides many built-in system traits,
iOS 17 λΆν° custom traits μ μ κ°λ₯
trait collections μ traits μ κ΄λ ¨ λ³μλ€μ ν¬ν¨
iOS 17 λΆν° trait collections μ new API μΆκ°
-
ν΄λ‘μ λ₯Ό μ·¨νλ μλ‘μ΄ initializer
- ν΄λ‘μ λ΄λΆμμ
mutableTraits
λ₯Ό λ°λλ€ - mutable container λ μλ‘μ΄ νλ‘ν μ½
UIMutableTraits
λ₯Ό conform - initializer κ° immutable
UITraitCollection
μΈμ€ν΄μ€λ₯Ό 리ν΄
- ν΄λ‘μ λ΄λΆμμ
-
κΈ°μ‘΄ trait collection μ κ°λ€μ μμ νμ¬ μλ‘μ΄ μΈμ€ν΄μ€λ₯Ό μμ±ν΄μ£Όλ
modifyingTraits
λ©μλ
μ§μ train collections λ₯Ό μ΄λ κ² μμ±ν΄μ μ¬μ© ν μλ μμ§λ§ λλΆλΆ μ°λ¦¬λ trait environment μμ trait collections μ μ»μ΄μ μ¬μ©νλ€.
trait environments μλ UIWindowScnene, UIWindow, UIPresentationController, UIViewController, UIView κ° μκ³ κ°κ° κ³ μ μ trait collection μ κ°λλ€.
- trait environment μ μ±μ ν΅ν΄ flow νλ trait hierarchy λ‘ μ°κ²°λμ΄ μλ€
- κ°κ°μ trait environment λ parent environment μ trait κ°λ€μ μμ λ°λλ€
iOS 17 μ΄μ
- ViewController λ Parent View Controller μ trait μ μ§μ μμ
- View Controller μ μν View λ€μ View Controller λ‘ λΆν° trait μμ
- View Controller μ μνμ§ μμ View λ€μ superView λ‘ λΆν° trait μμ
β View hierarchy μ μλ trait μ νλ‘μ°λ View Controller μ μν View μμ λκΈ΄λ€
iOS17 μμλ trait hierarchy λ₯Ό ν΅ν©
- View Controller λ View μ superview μμ trait μμ
β View Controller κ° μ΄μ view hierarchy μμ traits λ₯Ό μμ λ°κΈ° λλ¬Έμ, ViewController κ° μ λ°μ΄νΈλ trait μ λ°κΈ° μν΄μ ViewController μ View λ λ°λμ hierarchy μ μμ΄μΌ ν¨
β View κ° hierarchy μ μΆκ° λκΈ° μ μ ViewController μ trait collection μ μ κ·Όνλ©΄, ViewController λ trait μ μ΅μ κ°μ κ°μ Έμ¬ μ μλ€
viewWillAppear
λ View κ° hierarchy μ μΆκ° λκΈ° μ μ νμ νΈμΆλ¨
viewIsAppearing
viewIsAppearing
is called after viewWillAppear- View κ° hierarchy μ μΆκ°λλ©΄, View Controller, View λͺ¨λ μ΅μ triat collection μ κ°κ²λ¨
viewIsAppearing
is a drop-in replacement for nearly all cases where youβre using viewWillAppear- this new method back-deploys all the way to iOS 13.
- iOS 17 μμλ view trait μ λ°μ΄νΈμ μΌκ΄μ±κ³Ό μ±λ₯ ν₯μ
- View λ hierarchy μμ μμ λλ§ trait collection μ μλ°μ΄νΈν¨
- μΌλ¨ hierarchy μμ μμΌλ©΄, κ°κ°μ view λ layout μ΄ μνλκΈ° μ μ μ¦μ μμ μ trait collection λ§ μ λ°μ΄νΈ
- κ°μ₯ best practice λ layout λμ trait μ μ¬ μ©
- View λ
layoutSubViews()
λ©μλ μμμ traitCollection μ μ¬μ© layoutSubViews()
λ View μμsetNeedsLayout()
μ΄ νΈμΆ λ λλ§λ€ λμ νκΈ° λλ¬Έμ, μ¬λ¬λ² νΈμΆ ν λ λ°λ³΅λ μμ μ νΌν΄μΌν¨
- View λ
When to define a new custom trait
- μ¬λ¬ children μκ² λ°μ΄ν°λ₯Ό μ λ¬ ν΄μΌ ν λ
- ParentViewController β Muliple ChildViewControllers
- SuperView β all of its subviews
- λ€λ₯Έ μ»΄ν¬λνΈμ λ°μ΄ν°λ₯Ό μ λ¬ ν λ
- λ§μ λ μ΄μ΄λ€μ΄ nested λ κ²½μ°, direct connection μ μμλ
- ν¬ν¨νκ³ μλ 뷰컨 κ°μ environment μ 보λ€μ λ겨μ€λ
trait system μ΄ κ°λ ₯νκΈ΄ νμ§λ§, λ°μ΄ν° μ λ¬λ‘μ μ¬μ©νλ건 μ’μ§ μμ
μ±λ₯μ μν΄μλ μ§μ λ°μ΄ν°λ₯Ό μ λ¬νλκ²μ΄ μλ, κ°μ μΆκ°ν λ μ¬μ©ν΄μΌ νλ€
Custom trait
trait μ μ μ νλ©΄, λ°λ‘ UITraitCollection
, UIMutableTraits
μ μλ‘μ΄ API μ¬μ© κ°λ₯
struct μ체λ₯Ό ν€κ°μ²λΌ μ¬μ©
Custom trait μ μ ν λ extension λ μ¨μ£Όμ΄μΌ ν¨
μμμ μν₯μ μ£Όλ trait μ λΉμ©μ΄ λΉμΈκΈ° λλ¬Έμ, λλ¬Όκ² λ³ννλ trait μλ§ μ¬μ©
name β λλ²κ±°μ νμλλ μ΄λ¦
identifier
- μΈμ½λ© κ°μ μΆκ° κΈ°λ₯μ μ ν©ν trait μΌλ‘ λ§λ€μ΄μ€
- reverse-DNS ν¬λ©§ μ¬μ©
- μ±λ΄μμ μ λν¬ νλλ‘
ν λ§μ λ°λ₯Έ 컀μ€ν λ€μ΄λλ―Ή μ»¬λ¬ μ μ
- κ°λ¨ν struct λ enum κ°μ value types μ¬μ© Class κΈ°λ°μ trait λ νΌνκΈ°
- κ°μ₯ ν¨μ¨μ μΈ λ°μ΄ν° νμ
μ Bool, Int, Double, enum with Int raw value
enum
with Int raw value μ΄ κ°μ₯ μ μ©ν λ°μ΄ν° νμ - 컀μ€ν°λ¨Έ νμ
μ
Equatable
νλ‘ν μ½μ ꡬνν΄μΌν¨
Obj-C λ μ¬μ© κ°λ₯!
butβ¦ μ΅μ¨, Swift μ κ°κ° cutom trait μ μν΄μ£Όμ΄ν νλ€
Trait overrdies λ trait hierachy λ΄λΆμ λ°μ΄ν°λ₯Ό μμ νκΈ° μν 맀컀λμ¦μ΄λ€
iOS 17 λΆν°λ trait overrides λ₯Ό λ μ μ©νκΈ° μ¬μμ§
κ°κ°μ trait envirionment classesdp traitOverrides
νλ‘νΌν°κ° μκΉ
trait overrides λ νΈλ¦¬ ꡬ쑰 μμμ μ΄λ€ μμΉμμλ trait κ°μ λ°κΎΌλ€
μ΄ μ€ ν νκ²½μμ μ μ©λλ©΄ λͺ¨λ νμ κ³μΈ΅μ λ€ μ μ©
parent μμ μ μ©λ traitCollection μ μμλ°κ³ μλ child μλ μ μ©
μλ§ trait override κ° μ¦μ λ°μ λμ§ μμ μ μλ€
- View λ layout μ§μ μ trait collection μ μ
λ°μ΄νΈ νκΈ° λλ¬Έμ
view μ trait override μμ μ
layoutSubviews
μ κΉμ§λ λ°λ‘ λ°μλμ§ μλλ€ traitOverrides
νλ‘νΌν°λ override κ° μ μ©λμλμ§, μ κ±°λμλμ§λ νμΈν΄μ€
-
contains λ©μλλ‘ μ΄λ―Έ μλ κ±°λΌλ©΄ μ κ±°, μμΌλ©΄ μ μ©
-
traitOverrides
λ κ°μ μΈν νκΈ° μν input mechanism μ΄λ€β Trait value λ₯Ό read νκΈ° μν΄μλ
traitCollection
νλ‘νΌν°λ₯Ό μ¬μ©ν΄μΌ ν¨ -
override κ° μΈν λκ²μ΄ μμ λ,
traitOverrides
μμ μ½μΌλ©΄ exception λ°μ
- κ°κ°μ
traitOverride
λ λΉμ©μ΄ λ€κΈ° λλ¬Έμ κΌ νμν κ²½μ°μλ§ μ¬μ© - κ°μ μμ νλ κ²½μ°, μμ€ν
μ νμ trait collection μ λͺ¨λ μ
λ°μ΄νΈ ν΄μΌνκΈ° λλ¬Έμ,
traitOverride
λ₯Ό μμ νλ λΉλμλ₯Ό μ΅μν ν΄μΌν¨ - νμν κ³μΈ΅μ κ°μ₯ κ°κΉμ΄ κ³³μ μμ νμ
β **traitCollectionDidChange(_:)
is deprecated in iOS17**
- μ΄λ€ traitCollection μ care νλμ§ μμ€ν μ μμ§ λͺ»νκΈ° λλ¬Έμ λͺ¨λ traitCollection μ΄ λ³ν λλ§λ€ κ³μ λ©μλλ₯Ό μ½ν΄μΌν¨
- κ°κ°μ trait μ λν change λ₯Ό λ±λ‘νλ new API
- target action λ°©μ λλ clouse
- subclass μμ λ©μλλ₯Ό override ν νμ μκΈ° λλ¬Έμ μ΄λμλ changes λ₯Ό μ΅μ λΉ κ°λ₯
iOS 17 μ΄μ λ²μ μ λμν΄μ traitCollectionDidChange
λ₯Ό κ³μ μ¬μ©ν΄μΌ νλ€λ©΄,
μ΄λ€ νΉμ trait μ λν change λ₯Ό λ°λΌλ³΄κ³ μλμ§μ λν ꡬνμ μΆκ°ν΄μΌν¨
β¬οΈΒ iOS 17 β¬οΈ
Closure
-
UITraitHorizontalSizeClass
κ°μ λͺ¨λ μμ€ν trait μ λν μ¬λ³Όμ΄ μλ‘ μκΉ -
λͺ¨λ trait μ λν λ³νμ λν΄ νΈμΆλμ§ μκΈ° λλ¬Έμ λμ΄μ old, new trait κ°λ€μ λΉκ΅ν νμκ° μμ
-
trait μ λ³νκ° μκΈ΄ κ°μ²΄κ° 첫λ²μ§Έ νλΌλ―Έν°λ‘ μ λ¬λκΈ° λλ¬Έμ, weak λ νΌλ°μ€ μΊ‘μ³ ν νμ μμ΄ λ°λ‘ μ¬μ© ν μ μμ (
self: Self
λ‘ μ¬μ©ν΄μΌν¨) -
λ€λ₯Έ trait environment μ λν trait changes λ μ΅μ λΈ κ°λ₯
-
λ€λ₯Έ λ·°μμ μμ€ν trait, 컀μ€ν trait λͺ¨λ λ³νλ©΄ ν΄λ‘μ μ€ν
-
register ν λ·°μ νμ μ 첫λ²μ§Έ νλΌλ―Έν°λ‘ μ μ΄μ€
target-action
- target νλΌλ―Έν° μλ΅ κ°λ₯ (μλ΅νλ©΄ registerForTraitChange νΈμΆλλ κ°μ κ°μ²΄)
- view, previousTraitCollection νλΌλ―Έν°
### Semantic system trait sets
- μμ€ν λ€μ΄λλ―Ή 컬λ¬μ μν₯μ μ£Όλ system trait 리ν΄
- UIImage(named:) μ¬μ©ν΄μ μ΄λ―Έμ§ λ‘λμ system traits μ subset 리ν΄
- μ€μ λ‘ μμ‘΄νλ trait λ§ register λ±λ‘
- λ³νμ λν΄ μ¦μ λ°μνμ§ μλλ‘ ν΄λΌ
layoutSubviews λ©μλ λ΄λΆμμ trait μ μ¬μ©νλ€λ©΄,
setNeedsLayout
μ νΈμΆν΄μ trait change λ₯Ό invalidate ν΄λΌ ? β View κ° layoutSubviews λ₯Ό λ°κ³ , μ¦μ μ λ°μ΄νΈλ₯Ό νμ§ μλλ‘
UIKit β SwiftUI λ‘ λ°μ΄ν°λ₯Ό μ λ¬νλ λκΉ μλ μλ‘μ΄ λ°©λ²μ΄ μκΈ°κ² λ¨!
UIKit μ Custom trait μ SwiftUI μ environment keys μ λ§€μ° μ μ¬
λμ λΈλ¦Ώμ§ νκΈ° μν΄μλ UITraitBridgedEnvironmentKey
νλ‘ν μ½λ§ μ±νν΄μ£Όλ©΄ λ¨
UIKit β SwiftUI
SwiftUI β UIKit