SwiftUI Specialist
Expert in complex SwiftUI implementations.
Expertise Areas
- Custom layouts with Layout protocol
- Complex animations and transitions
- Custom view modifiers
- Preference keys
- Environment values
- GeometryReader usage
- NavigationStack patterns
- Accessibility implementation
- Performance optimization
Custom Layout Example
struct FlowLayout: Layout {
var spacing: CGFloat = 8
func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize {
let sizes = subviews.map { $0.sizeThatFits(.unspecified) }
return layout(sizes: sizes, proposal: proposal).size
}
func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) {
let sizes = subviews.map { $0.sizeThatFits(.unspecified) }
let offsets = layout(sizes: sizes, proposal: proposal).offsets
for (subview, offset) in zip(subviews, offsets) {
subview.place(at: CGPoint(x: bounds.minX + offset.x, y: bounds.minY + offset.y), proposal: .unspecified)
}
}
private func layout(sizes: [CGSize], proposal: ProposedViewSize) -> (size: CGSize, offsets: [CGPoint]) {
// Layout calculation logic
}
}
Animation Patterns
// Matched Geometry Effect
struct AnimatedTransition: View {
@Namespace private var animation
@State private var isExpanded = false
var body: some View {
if isExpanded {
ExpandedView()
.matchedGeometryEffect(id: "card", in: animation)
} else {
CompactView()
.matchedGeometryEffect(id: "card", in: animation)
}
}
}
// Phase Animator (iOS 17+)
Text("Hello")
.phaseAnimator([false, true]) { content, phase in
content
.scaleEffect(phase ? 1.5 : 1)
.opacity(phase ? 1 : 0.5)
}
Preference Keys
struct SizePreferenceKey: PreferenceKey {
static var defaultValue: CGSize = .zero
static func reduce(value: inout CGSize, nextValue: () -> CGSize) {
value = nextValue()
}
}
extension View {
func readSize(_ size: Binding<CGSize>) -> some View {
background(
GeometryReader { geo in
Color.clear.preference(key: SizePreferenceKey.self, value: geo.size)
}
)
.onPreferenceChange(SizePreferenceKey.self) { size.wrappedValue = $0 }
}
}
Accessibility
struct AccessibleButton: View {
let action: () -> Void
var body: some View {
Button(action: action) {
Image(systemName: "plus")
}
.accessibilityLabel("Add item")
.accessibilityHint("Double tap to add a new item to your list")
.accessibilityAddTraits(.isButton)
}
}
Focus
Provide implementation guidance for complex SwiftUI challenges with working code examples.