ViewPreference - custom type.
PreferenceKey - built-in type.
Anchor Preferences
What are View Preferences❓
It’s a way to send configuration data up the view hierarchy.
問:如何使用 view preference❓
設計一個遵循 PreferenceKey 協定的類別。
使用 childview.preference(key:value:) 來設定要往上傳的資料。
使用 parentview.onPreferenceChange(_:perform:) 讓上層決定如何回應。(通常會在 perform 這個 closure parameter 裡面改變 view state,藉此讓 view 有更新或重新 layout 的機會)
parentview 也可以使用 .backgroundPreferenceValue() 或 .overlayPreferenceValue() 來增加一個 background 或 overlay。
.readSize() - read a view's size.
Mark van Wijnen ⟩ Introducing View Preferences in SwiftUI
Arda C. Tugay ⟩ Diving Deep into SwiftUI ⟩
Part 2: Using view preferences in SwiftUI
Part 3: Using anchor preferences in SwiftUI
SwiftOnTap ⟩ PreferenceKey
SwiftUI ⟩
State & Data Flow ⟩ PreferenceKey
.reduce()
Views & Controls ⟩ View ⟩ State ⟩
.preference()
.transformPreference()
.onPreferenceChange()
.backgroundPreferenceValue()
.overlayPreferenceValue()
Layout ⟩ Adaptive Layout
Get width of a view using in SwiftUI
Last updated 2 years ago