Modifiers, Frames and Spacing
What is a Modifier?
A modifier is a method that wraps a view and returns a new view with a change. You chain them one after another.
swift
Text("Buy now")
.font(.headline)
.foregroundStyle(.white)
.padding()
.background(.blue)
.clipShape(RoundedRectangle(cornerRadius: 12))
Order Matters!
swift
// 1. Background first, then padding: color only behind the text
Text("Hi").background(.yellow).padding()
// 2. Padding first, then background: color covers the padded area
Text("Hi").padding().background(.yellow)
Think in layers: Each modifier wraps the result of the previous one like layers of an onion. The background applies to whatever is inside it at that moment.
Padding
swift
Text("A").padding() // all sides, default amount
Text("B").padding(20) // 20 points
Text("C").padding(.horizontal, 24)
Text("D").padding(.top, 8)
Text("E").padding([.leading, .bottom], 12)
Frame
swift
Text("Fixed").frame(width: 120, height: 44)
Button("Wide") {}
.frame(maxWidth: .infinity) // fill the available width
.padding()
.background(.blue)
.foregroundStyle(.white)
Rectangle()
.frame(minWidth: 100, maxWidth: 300, minHeight: 50)
Background, Overlay, Border
swift
Text("Card")
.padding()
.background(.white, in: RoundedRectangle(cornerRadius: 12))
.overlay(
RoundedRectangle(cornerRadius: 12).stroke(.gray.opacity(0.4), lineWidth: 1)
)
.shadow(color: .black.opacity(0.1), radius: 8, y: 4)
Circle().fill(.red).frame(width: 40).overlay(Text("3").foregroundStyle(.white))
Opacity, Rotation, Offset, Scale
swift
Image(systemName: "arrow.up")
.font(.largeTitle)
.rotationEffect(.degrees(45))
.scaleEffect(1.5)
.opacity(0.8)
.offset(x: 10, y: -5)
Reusable Style with ViewModifier
swift
struct CardStyle: ViewModifier {
func body(content: Content) -> some View {
content
.padding()
.background(.background, in: RoundedRectangle(cornerRadius: 16))
.shadow(color: .black.opacity(0.1), radius: 6, y: 3)
}
}
extension View {
func cardStyle() -> some View { modifier(CardStyle()) }
}
Text("Hello").cardStyle()
Extract Subviews
When a view gets long, split it into smaller structs. Smaller views are easier to read, preview and reuse.
swift
struct PriceTag: View {
let amount: Double
var body: some View {
Text(amount, format: .currency(code: "USD"))
.font(.headline)
.padding(8)
.background(.green.opacity(0.2), in: Capsule())
}
}
Tip: Format numbers and dates with
Text(value, format: ...). It follows the user's language and region automatically.
Conclusion
Modifiers customize views and the order changes the outcome. Extract repeated styling into a ViewModifier and split big views into subviews.
Quick Summary: Modifiers wrap views in order. Use padding, frame, background and overlay, and reuse styles with ViewModifier.

