Bird
Raised Fist0
iOS Swiftmobile~20 mins

Custom ViewModifiers in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
SwiftUI Modifier Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate
2:00remaining
What is the visual result of this Custom ViewModifier?

Consider this SwiftUI code defining a custom modifier and applying it to a Text view. What will the text look like?

iOS Swift
struct Highlight: ViewModifier {
  func body(content: Content) -> some View {
    content
      .padding(10)
      .background(Color.yellow)
      .cornerRadius(8)
  }
}

Text("Hello SwiftUI")
  .modifier(Highlight())
AText with red background and no padding
BText with padding but transparent background
CText with yellow background but no corner radius
DText with yellow background, padding around text, and rounded corners
Attempts:
2 left
💡 Hint

Look at the order of modifiers inside the body function.

❓ lifecycle
intermediate
1:30remaining
When is the body of a Custom ViewModifier called?

In SwiftUI, when does the body(content:) method of a ViewModifier get executed?

AOnly when the modifier is explicitly reapplied in code
BOnly once when the view is first created
CEvery time the view's state changes and the view needs to update
DNever, it is only a placeholder method
Attempts:
2 left
💡 Hint

Think about how SwiftUI updates views reactively.

📝 Syntax
advanced
2:30remaining
Which option correctly defines a Custom ViewModifier with a parameter?

Choose the correct Swift code for a ViewModifier that takes a Color parameter to set the background color.

A
struct ColoredBackground: ViewModifier {
  var color: Color
  func body(content: Content) -> some View {
    content.background(color)
  }
}
B
struct ColoredBackground: ViewModifier {
  func body(content: Content, color: Color) -> some View {
    content.background(color)
  }
}
C
struct ColoredBackground: ViewModifier {
  var color: Color
  func body(content: Content) -> View {
    content.background(color)
  }
}
D
struct ColoredBackground: ViewModifier {
  var color: Color
  func body(content: Content) -> some View {
    content.background(Color)
  }
}
Attempts:
2 left
💡 Hint

Remember the body method signature and how to use parameters in structs.

🔧 Debug
advanced
2:00remaining
Why does this Custom ViewModifier cause a runtime crash?

Given this code, why does the app crash when applying the modifier?

iOS Swift
struct CrashModifier: ViewModifier {
  func body(content: Content) -> some View {
    content
      .background(nil)
  }
}

Text("Crash")
  .modifier(CrashModifier())
AThe modifier is missing a required initializer
BPassing nil to background causes a runtime crash because background expects a View, not nil
CText cannot have a background modifier
DThe modifier must return a Text view explicitly
Attempts:
2 left
💡 Hint

Check the parameter type for the background modifier.

🧠 Conceptual
expert
1:30remaining
What is the benefit of using a Custom ViewModifier in SwiftUI?

Which of these best explains why you would create a custom ViewModifier?

ATo reuse a set of view styling and behavior across multiple views easily
BTo replace the need for creating new Views entirely
CTo improve app performance by compiling views faster
DTo allow views to modify their own state directly
Attempts:
2 left
💡 Hint

Think about code reuse and consistency in UI design.

Practice

(1/5)
1. What is the main purpose of creating a custom ViewModifier in SwiftUI?
easy
A. To reuse a set of view styles and behaviors easily
B. To create a new type of view from scratch
C. To handle user input events directly
D. To manage app navigation between screens

Solution

  1. Step 1: Understand what a ViewModifier does

    A ViewModifier bundles styling and behavior changes for views.
  2. Step 2: Identify the main use

    It helps reuse these styles easily across many views.
  3. Final Answer:

    To reuse a set of view styles and behaviors easily -> Option A
  4. Quick Check:

    Custom ViewModifiers = reuse styles [OK]
Hint: Think: reuse styles and behaviors with one modifier [OK]
Common Mistakes:
  • Confusing ViewModifier with creating new views
  • Thinking it handles navigation or input directly
  • Assuming it manages app state
2. Which of the following is the correct way to apply a custom ViewModifier named Highlight to a Text view?
easy
A. Text("Hello").modifier(Highlight())
B. Text("Hello").applyModifier(Highlight)
C. Text("Hello").useModifier(Highlight())
D. Text("Hello").addModifier(Highlight)

Solution

  1. Step 1: Recall the correct method name

    The method to apply a ViewModifier is .modifier().
  2. Step 2: Check syntax for applying Highlight

    It should be .modifier(Highlight()) with parentheses to create the modifier instance.
  3. Final Answer:

    Text("Hello").modifier(Highlight()) -> Option A
  4. Quick Check:

    Use .modifier() with instance [OK]
Hint: Remember: .modifier() takes an instance with () [OK]
Common Mistakes:
  • Using wrong method names like applyModifier or addModifier
  • Forgetting parentheses after modifier name
  • Passing type instead of instance
3. Given this custom ViewModifier:
struct RedTitle: ViewModifier {
  func body(content: Content) -> some View {
    content
      .font(.title)
      .foregroundColor(.red)
  }
}
What will this code display?
Text("Welcome").modifier(RedTitle())
medium
A. Text "Welcome" with no changes
B. Text "Welcome" in default color with title font
C. Text "Welcome" in red color with default font
D. Text "Welcome" in red color with title font

Solution

  1. Step 1: Analyze the RedTitle modifier

    It sets the font to title and text color to red.
  2. Step 2: Apply modifier to Text("Welcome")

    The text will appear with title font size and red color.
  3. Final Answer:

    Text "Welcome" in red color with title font -> Option D
  4. Quick Check:

    RedTitle = red + title font [OK]
Hint: Look at modifier body: font and color changes [OK]
Common Mistakes:
  • Ignoring the font change
  • Assuming default color remains
  • Confusing modifier effects
4. What is wrong with this custom ViewModifier code?
struct BlueBackground: ViewModifier {
  func body(content: Content) -> some View {
    content.background(Color.blue)
  }

  func body(content: Content) -> some View {
    content.padding()
  }
}
medium
A. Background modifier is not allowed inside ViewModifier
B. Two body functions cause a duplicate method error
C. Missing return keyword in body functions
D. Padding modifier must be applied before background

Solution

  1. Step 1: Check method definitions in struct

    There are two body(content:) methods defined, which is not allowed.
  2. Step 2: Understand Swift rules for method overloading

    Swift does not allow two methods with the same signature in one struct.
  3. Final Answer:

    Two body functions cause a duplicate method error -> Option B
  4. Quick Check:

    Duplicate body() methods = error [OK]
Hint: Only one body(content:) allowed per ViewModifier [OK]
Common Mistakes:
  • Trying to overload body method
  • Forgetting return keyword (not needed here)
  • Misunderstanding modifier order rules
5. You want to create a custom ViewModifier that adds a rounded border only if a Boolean highlight is true. Which of these implementations correctly applies the border conditionally?
hard
A. struct ConditionalBorder: ViewModifier { var highlight: Bool func body(content: Content) -> some View { if highlight { content.border(Color.green, width: 2) } else { content } } }
B. struct ConditionalBorder: ViewModifier { var highlight: Bool func body(content: Content) -> some View { content.border(highlight ? Color.green : Color.clear, width: 2) } }
C. struct ConditionalBorder: ViewModifier { var highlight: Bool func body(content: Content) -> some View { content .border(Color.green, width: highlight ? 2 : 0) } }
D. struct ConditionalBorder: ViewModifier { var highlight: Bool func body(content: Content) -> some View { content .border(Color.green, width: highlight) } }

Solution

  1. Step 1: Eliminate compile errors

    struct ConditionalBorder: ViewModifier { var highlight: Bool func body(content: Content) -> some View { if highlight { content.border(Color.green, width: 2) } else { content } } } returns different view types in if-else (compiler error). struct ConditionalBorder: ViewModifier { var highlight: Bool func body(content: Content) -> some View { content .border(Color.green, width: highlight) } } passes Bool to width (CGFloat expected, type error).
  2. Step 2: Compare remaining options

    struct ConditionalBorder: ViewModifier { var highlight: Bool func body(content: Content) -> some View { content.border(highlight ? Color.green : Color.clear, width: 2) } } uses Color.clear with fixed width 2: invisible but expands frame (layout shift). struct ConditionalBorder: ViewModifier { var highlight: Bool func body(content: Content) -> some View { content .border(Color.green, width: highlight ? 2 : 0) } } uses conditional width 2/0: border only when true, no layout shift.
  3. Final Answer:

    content.border(Color.green, width: highlight ? 2 : 0) -> Option C
  4. Quick Check:

    Conditional width = correct conditional border [OK]
Hint: Ternary on width for conditional border without layout shift [OK]
Common Mistakes:
  • Returning different view types in if-else inside body
  • Passing Bool where CGFloat is expected
  • Using Color.clear with fixed width (layout shift)
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard