Note: The Listed platform will permanently shut down December 31, 2026. Your data published on Listed will still be available in your personal Standard Notes account. Learn more

SwiftUI-Day-6-7

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

  1. Content modifiers first: font, foregroundColor
  2. Layout modifiers: padding, frame
  3. Visual modifiers: background, border, shadow
  4. 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:

  1. padding → background → frame
  2. frame → padding → background
  3. 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

  1. Ignoring modifier order - Always consider how modifiers stack
  2. Over-styling - Keep designs clean and consistent
  3. Forgetting .resizable() - Images won’t scale without it
  4. Not testing on different screen sizes - Use frame modifiers wisely
  5. 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.


You'll only receive email when they publish something new.

More from LearnWithAI
All posts