0
0
iOS Swiftmobile~10 mins

Custom ViewModifiers in iOS Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a simple custom ViewModifier named RedTitle that makes text red and bold.

iOS Swift
struct RedTitle: ViewModifier {
  func body(content: Content) -> some View {
    content
      .foregroundColor([1])
      .bold()
  }
}
Drag options to blanks, or click blank then click option'
AUIColor.red
B.red
CColor.red
Dred
Attempts:
3 left
💡 Hint
Common Mistakes
Using UIColor instead of Color for SwiftUI colors.
Trying to use .red without Color prefix.
2fill in blank
medium

Complete the code to apply the custom ViewModifier RedTitle to a Text view.

iOS Swift
Text("Hello World")
  .[1](RedTitle())
Drag options to blanks, or click blank then click option'
Amodifier
Bstyle
Cuse
Dapply
Attempts:
3 left
💡 Hint
Common Mistakes
Using .apply or .use which do not exist in SwiftUI.
Trying to call the modifier without parentheses.
3fill in blank
hard

Fix the error in the custom ViewModifier by completing the missing return type for the body function.

iOS Swift
struct BlueBackground: ViewModifier {
  func body(content: Content) -> [1] {
    content
      .background(Color.blue)
  }
}
Drag options to blanks, or click blank then click option'
Asome View
BView
CVoid
DContent
Attempts:
3 left
💡 Hint
Common Mistakes
Returning Void or Content instead of some View.
Omitting the return type.
4fill in blank
hard

Fill both blanks to create a reusable ViewModifier named RoundedBorder that adds a rounded border with a given color and width.

iOS Swift
struct RoundedBorder: ViewModifier {
  var color: Color
  var width: CGFloat

  func body(content: Content) -> some View {
    content
      .overlay(
        RoundedRectangle(cornerRadius: [1])
          .stroke(color, lineWidth: [2])
      )
  }
}
Drag options to blanks, or click blank then click option'
A10
B5
C15
D20
Attempts:
3 left
💡 Hint
Common Mistakes
Using very large corner radius or line width values.
Mixing up corner radius and line width values.
5fill in blank
hard

Fill all three blanks to create a View extension that applies the RoundedBorder modifier with default color and width.

iOS Swift
extension View {
  func roundedBorder() -> some View {
    self.modifier(RoundedBorder(color: [1], width: [2]))
  }

  func roundedBorder(color: Color, width: CGFloat) -> some View {
    self.modifier(RoundedBorder(color: color, width: [3]))
  }
}
Drag options to blanks, or click blank then click option'
AColor.gray
B3
Cwidth
DColor.black
Attempts:
3 left
💡 Hint
Common Mistakes
Using a literal number instead of the parameter in the second method.
Mixing up default color values.