skip to content

Modifiers and Layout Negotiation

Modifiers wrap views in order, and layout negotiates sizes by propose, choose and place. Expect padding then background puzzles, frame questions and GeometryReader traps in every loop.

on this pageshow

explore

questions

page 2 of 2

In a custom SwiftUI Layout, when do makeCache and updateCache run, and what is safe to keep in the layout cache?

level: seniorimportance: nice to knowfreq 20%

basics

~10 s

makeCache(subviews:) builds storage that a layout's methods receive inout; SwiftUI calls updateCache(_:subviews:) when the subviews change, and its default rebuilds through makeCache. Keep subview-derived measurements there and key anything that depends on the proposal.

open as a page

In SwiftUI, how would you build a tab indicator that slides to match the width and position of the selected tab label?

level: seniorimportance: nice to knowfreq 25%

basics

~20 s

Have the selected label publish its bounds with anchorPreference(key:value:transform:) as an Anchor<CGRect>, then in the tab row's overlayPreferenceValue add a GeometryReader that resolves it with proxy[anchor] and draws the indicator there, animated on the selection.

open as a page

showing 31–32 of 32