SwiftUI-Day-6-7
September 8, 2025•1,184 words
SwiftUI Basic Views and Modifiers - Days 6-7 Learning Guide
Learning Objectives
By the end of these two days, you will:
- Master the four fundamental SwiftUI views: Text, Image, Button, and Spacer
- Understand how to apply styling modifiers effectively
- Learn the critical importance of modifier order
- Build confidence in creating visually appealing user interfaces
Day 6: Fundamental Views
Text View
The Text view displays static text content and is one of the most commonly used views.
Basic Usage:
Text("Hello, SwiftUI!")
Text("Multi-line text content that will wrap automatically when it becomes too long for the available space")
Key Properties:
- Content: String, LocalizedStringKey, or formatted data
- Styling: Font, color, alignment, line spacing
- Behavior: Automatic text wrapping, accessibility support
Common Modifiers:
.font(.title)- Sets text size and weight.foregroundColor(.blue)- Changes text color.multilineTextAlignment(.center)- Aligns multi-line text.lineLimit(3)- Restricts number of lines
Image View
Displays images from your app bundle, SF Symbols, or system images.
Basic Usage:
Image("photo-name") // From app bundle
Image(systemName: "heart") // SF Symbol
Image(systemName: "star.fill")
Key Properties:
- Source: App bundle images or SF Symbols
- Rendering: Template, original, or automatic
- Scaling: Aspect fit, aspect fill, or stretch
Common Modifiers:
.resizable()- Allows image to be resized.scaledToFit()- Maintains aspect ratio, fits container.scaledToFill()- Maintains aspect ratio, fills container.frame(width: 100, height: 100)- Sets specific dimensions
Button View
Interactive element that responds to user taps and triggers actions.
Basic Usage:
Button("Tap Me") {
print("Button tapped!")
}
Button(action: {
// Action code here
}) {
Text("Custom Button")
}
Key Properties:
- Action: Closure executed when tapped
- Label: Text, Image, or custom view content
- State: Normal, pressed, disabled
Common Modifiers:
.buttonStyle(.bordered)- Applies system button style.disabled(true)- Disables user interaction.foregroundColor(.white)- Changes button text color.background(Color.blue)- Sets button background
Spacer View
Flexible space that expands to fill available area, pushing other views apart.
Basic Usage:
HStack {
Text("Left")
Spacer()
Text("Right")
}
Key Properties:
- Flexibility: Expands to fill available space
- Direction: Works in both horizontal and vertical stacks
- Priority: Lower layout priority than content views
Common Usage Patterns:
- Creating space between elements
- Pushing content to edges
- Centering content with dual spacers
Day 7: Styling Modifiers
Color Modifiers
Control the visual appearance of your views with color.
Foreground Colors:
Text("Colored Text")
.foregroundColor(.red)
.foregroundColor(Color.blue)
.foregroundColor(Color(red: 0.8, green: 0.2, blue: 0.6))
Background Colors:
Text("Background")
.background(Color.yellow)
.background(Color.secondary.opacity(0.3))
Font Modifiers
Customize text appearance with various font options.
System Fonts:
Text("Large Title").font(.largeTitle)
Text("Title").font(.title)
Text("Body").font(.body)
Text("Caption").font(.caption)
Custom Fonts:
Text("Custom Font")
.font(.system(size: 24, weight: .bold, design: .rounded))
.fontWeight(.semibold)
.italic()
Padding Modifiers
Add space around views for better visual separation.
Basic Padding:
Text("Padded Text")
.padding() // All sides, default amount
.padding(20) // All sides, custom amount
.padding(.horizontal) // Left and right only
.padding(.top, 10) // Specific side and amount
Frame Modifiers
Control the size and alignment of views within their container.
Size Control:
Text("Fixed Size")
.frame(width: 200, height: 100)
Text("Minimum Size")
.frame(minWidth: 100, minHeight: 50)
Text("Maximum Size")
.frame(maxWidth: .infinity, maxHeight: 200)
Alignment:
Text("Aligned")
.frame(width: 200, height: 100, alignment: .topLeading)
Border and Shape Modifiers
Add visual boundaries and shape to your views.
Borders:
Text("Bordered")
.border(Color.black, width: 2)
Text("Custom Border")
.overlay(
RoundedRectangle(cornerRadius: 8)
.stroke(Color.blue, lineWidth: 2)
)
Backgrounds with Shapes:
Text("Rounded Background")
.background(
RoundedRectangle(cornerRadius: 10)
.fill(Color.blue)
)
Critical Concept: Modifier Order
The order in which you apply modifiers is extremely important and can dramatically change the appearance of your views.
Why Order Matters
Each modifier creates a new view that wraps the previous view. Think of modifiers as layers being added one by one.
Examples of Order Impact
Background then Padding:
Text("Order Test")
.background(Color.yellow) // Background applies to text only
.padding() // Padding added outside background
Result: Yellow background around text, then padding around the background.
Padding then Background:
Text("Order Test")
.padding() // Padding added to text
.background(Color.yellow) // Background covers text + padding
Result: Yellow background covers the text and its padding area.
Frame then Background vs Background then Frame:
// Frame first
Text("Frame First")
.frame(width: 200, height: 100)
.background(Color.red)
// Background first
Text("Background First")
.background(Color.red)
.frame(width: 200, height: 100)
Best Practices for Modifier Order
- Content modifiers first: font, foregroundColor
- Layout modifiers: padding, frame
- Visual modifiers: background, border, shadow
- Interaction modifiers: onTapGesture, disabled
Recommended Order:
Text("Well-Ordered")
.font(.title) // 1. Content
.foregroundColor(.white) // 1. Content
.padding() // 2. Layout
.frame(width: 200) // 2. Layout
.background(Color.blue) // 3. Visual
.cornerRadius(10) // 3. Visual
.shadow(radius: 5) // 3. Visual
.onTapGesture { } // 4. Interaction
Practical Exercises
Exercise 1: Basic Views Practice
Create a view containing:
- A title using Text with large font
- An SF Symbol image (star, heart, etc.)
- A button that prints to console when tapped
- Use Spacer to arrange elements vertically
Exercise 2: Styling Challenge
Style a Text view with:
- Custom font size and weight
- Colored text and background
- Padding and rounded corners
- A subtle shadow
Exercise 3: Modifier Order Experiment
Create the same text with modifiers in different orders:
- padding → background → frame
- frame → padding → background
- background → padding → frame
Observe and document the differences.
Exercise 4: Button Styling
Create three different button styles:
- A bordered system button
- A custom button with background color and rounded corners
- An image-based button using SF Symbols
Common Mistakes to Avoid
- Ignoring modifier order - Always consider how modifiers stack
- Over-styling - Keep designs clean and consistent
- Forgetting .resizable() - Images won’t scale without it
- Not testing on different screen sizes - Use frame modifiers wisely
- Inconsistent spacing - Use consistent padding values
Key Takeaways
- Text, Image, Button, and Spacer are fundamental building blocks
- Modifiers transform views by wrapping them in new functionality
- Order matters critically - each modifier creates a new view layer
- Consistent styling creates professional-looking interfaces
- Experimentation is key to understanding modifier behavior
Further Reading
- Apple’s SwiftUI Documentation: Views and Modifiers
- SwiftUI Modifier Reference Guide
- Human Interface Guidelines for iOS
- Accessibility in SwiftUI
Day 6-7 Completion Checklist
- [ ] Understand Text view properties and basic usage
- [ ] Can display and resize images properly
- [ ] Know how to create interactive buttons
- [ ] Understand Spacer’s role in layout
- [ ] Can apply color and font modifiers
- [ ] Understand padding and frame modifiers
- [ ] Grasp the critical importance of modifier order
- [ ] Complete all practical exercises
- [ ] Experiment with different modifier combinations
Noted:
This generated by AI. This may be not correct information.