0
0
iOS Swiftmobile~20 mins

Custom ViewModifiers in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 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.