All Posts
Post
Ad‑hoc frame math sprinkled through view bodies breeds flaky, non‑deterministic layout. Pull spatial rules into focused Layout types to get deterministic sizing and easier tests.
- Centralize placement math behind a small
Layoutso you can callsizeThatFitsinXCTestand assert behavior across rotations or Dynamic Type changes. - Use a minimal
Cacheinside theLayoutto avoid repeated expensive measurements; document what invalidates it (for example: content changes,\.sizeCategory, or orientation). - Combine custom
LayoutwithLazyVStack/LazyHStackfor large collections to avoid measuring every child eagerly. - Instrument
sizeThatFitsandplaceSubviewswith Instruments or runtime timing (orOSSignposter) during rollout to spot hot paths.
When screens include large UIKit surfaces or third‑party UIViews, prefer incremental embedding with UIHostingController rather than rewriting everything at once.
What tradeoff surprised you most when adopting Layout, or how have you handled cache invalidation and flaky measurements migrating UIKit screens into SwiftUI? Share a concrete example.