# Preferences

{% tabs %}
{% tab title="🔴 主題" %}
* [viewpreference](preferences/viewpreference "mention") - custom type.
* [preferencekey](preferences/preferencekey "mention") - built-in type.
* [anchor-preferences](preferences/anchor-preferences "mention")
{% endtab %}

{% tab title="⭐️ 重點" %}
{% hint style="info" %}
What are **View Preferences**❓

* It’s a way to **send configuration data** <mark style="color:red;">**up**</mark> the <mark style="color:orange;">**view hierarchy**</mark>.
{% endhint %}

{% hint style="info" %}
問：如何使用 view preference❓

* <mark style="color:red;">**設計**</mark>一個遵循 [PreferenceKey](preferences/preferencekey) 協定的類別。
* 使用 <mark style="color:orange;">**childview**</mark>.[preference(key:value:)](https://developer.apple.com/documentation/swiftui/view/preference\(key:value:\)) 來<mark style="color:red;">**設定要往上傳的資料**</mark>。
* 使用 <mark style="color:green;">**parentview**</mark>.[onPreferenceChange(\_:perform:)](https://developer.apple.com/documentation/swiftui/view/onpreferencechange\(_:perform:\)) 讓<mark style="color:red;">**上層決定如何回應**</mark>。(通常會在 [<mark style="color:purple;">**perform**</mark>](https://developer.apple.com/documentation/swiftui/view/onpreferencechange\(_:perform:\)) 這個 **closure parameter** 裡面改變 **view** [**state**](../view/state/value/state)，藉此讓 view 有<mark style="color:red;">**更新**</mark>或重新 <mark style="color:red;">**layout**</mark> 的機會)
* <mark style="color:green;">**parentview**</mark> 也可以使用 .[backgroundPreferenceValue](https://developer.apple.com/documentation/swiftui/view/backgroundpreferencevalue\(_:_:\))() 或 .[overlayPreferenceValue](https://developer.apple.com/documentation/swiftui/view/overlaypreferencevalue\(_:_:\))() 來增加一個 <mark style="color:orange;">**background**</mark> 或 <mark style="color:orange;">**overlay**</mark>。
{% endhint %}
{% endtab %}

{% tab title="💈範例" %}
* [.readsize](../view/view/.readsize "mention") - read a view's size.
{% endtab %}

{% tab title="📗 參考" %}
* [x] Mark van Wijnen ⟩ [Introducing View Preferences in SwiftUI](https://crystalminds.medium.com/introducing-view-preferences-in-swiftui-e193c346b68d)
* [ ] Arda C. Tugay ⟩ Diving Deep into SwiftUI ⟩&#x20;
  * [ ] [Part 2: Using view preferences in SwiftUI](https://betterprogramming.pub/diving-deep-into-swiftui-using-view-preferences-part-2-of-5-ebd8a91d6c3e)
  * [ ] [Part 3: Using anchor preferences in SwiftUI](https://betterprogramming.pub/diving-deep-into-swiftui-part-3-853173868f07)
{% endtab %}

{% tab title="📘 手冊" %}
* SwiftOnTap ⟩ [PreferenceKey](https://swiftontap.com/preferencekey)
* [SwiftUI](https://developer.apple.com/documentation/swiftui)  ⟩ &#x20;
  * [State & Data Flow](https://developer.apple.com/documentation/swiftui/state-and-data-flow)  ⟩  [**PreferenceKey**](https://developer.apple.com/documentation/swiftui/preferencekey)
    * .[**reduce**()](https://developer.apple.com/documentation/swiftui/preferencekey/reduce\(value:nextvalue:\))
  * [Views & Controls](https://developer.apple.com/documentation/swiftui/views-and-controls)  ⟩  [View](https://developer.apple.com/documentation/swiftui/view)  ⟩  [State](https://developer.apple.com/documentation/swiftui/view-state)  ⟩ &#x20;
    * .[**preference**()](https://developer.apple.com/documentation/swiftui/view/preference\(key:value:\))
    * .[transformPreference](https://developer.apple.com/documentation/swiftui/view/transformpreference\(_:_:\))()
    * .[**onPreferenceChange**()](https://developer.apple.com/documentation/swiftui/view/onpreferencechange\(_:perform:\))
    * .[backgroundPreferenceValue](https://developer.apple.com/documentation/swiftui/view/backgroundpreferencevalue\(_:_:\))()
    * .[overlayPreferenceValue](https://developer.apple.com/documentation/swiftui/view/overlaypreferencevalue\(_:_:\))()
{% endtab %}

{% tab title="👥 相關" %}
* [layout](../view/layout "mention") ⟩ [adaptive-layout](../view/layout/adaptive-layout "mention")
{% endtab %}

{% tab title="🗣 討論" %}
* [Get width of a view using in SwiftUI](https://stackoverflow.com/a/59783290/5409815)
{% endtab %}
{% endtabs %}
