0
0
iOS Swiftmobile~20 mins

ScrollView in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ScrollView Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
ScrollView Content Size Behavior
What will happen if you place a VStack with fixed height inside a ScrollView without setting the ScrollView frame height?
iOS Swift
ScrollView {
  VStack {
    Rectangle().frame(height: 1000)
  }
}
AThe ScrollView will not scroll and only show content fitting the screen height.
BThe ScrollView will scroll vertically to show all content.
CThe ScrollView will crash due to ambiguous height constraints.
DThe ScrollView will scroll horizontally instead of vertically.
Attempts:
2 left
💡 Hint
Think about how ScrollView calculates its content size based on child views.
lifecycle
intermediate
2:00remaining
ScrollView and State Updates
If you update a @State variable inside a ScrollView's content, what happens to the ScrollView's position?
iOS Swift
struct ContentView: View {
  @State private var count = 0
  var body: some View {
    ScrollView {
      VStack {
        Text("Count: \(count)")
        Button("Increment") { count += 1 }
      }
    }
  }
}
AThe ScrollView resets to the top when the state changes.
BThe ScrollView scrolls to the bottom automatically after the state changes.
CThe ScrollView keeps its current scroll position after the state changes.
DThe ScrollView crashes due to state update inside its content.
Attempts:
2 left
💡 Hint
Think about how SwiftUI preserves scroll position on state updates.
navigation
advanced
2:00remaining
Programmatic ScrollView Scrolling
Which code snippet correctly scrolls a ScrollView to the bottom when a button is tapped?
A
ScrollViewReader { proxy in
  VStack {
    Button("Scroll to Bottom") {
      proxy.scrollTo("bottom", anchor: .bottom)
    }
    ScrollView {
      VStack {
        ForEach(0..<50) { i in
          Text("Item \(i)").id(i)
        }
        Text("Bottom").id("bottom")
      }
    }
  }
}
B
ScrollView {
  VStack {
    ForEach(0..<50) { i in
      Text("Item \(i)")
    }
  }
  Button("Scroll to Bottom") {
    scrollToBottom()
  }
}
func scrollToBottom() {
  // No ScrollViewReader used
}
C
ScrollViewReader { proxy in
  ScrollView {
    VStack {
      ForEach(0..<50) { i in
        Text("Item \(i)").id(i)
      }
      Button("Scroll to Bottom") {
        proxy.scrollTo(50, anchor: .bottom)
      }
    }
  }
}
D
ScrollViewReader { proxy in
  ScrollView {
    VStack {
      ForEach(0..<50) { i in
        Text("Item \(i)").id(i)
      }
    }
  }
  Button("Scroll to Bottom") {
    proxy.scrollTo(49, anchor: .bottom)
  }
}
Attempts:
2 left
💡 Hint
Remember to use ScrollViewReader and assign IDs to scroll targets.
📝 Syntax
advanced
2:00remaining
Correct ScrollView Axis Syntax
Which code snippet correctly creates a horizontal ScrollView in SwiftUI?
A
ScrollView(.horizontal, showsIndicators: false) {
  VStack {
    Text("One")
    Text("Two")
  }
}
B
ScrollView(axis: .horizontal) {
  HStack {
    Text("One")
    Text("Two")
  }
}
C
ScrollView(direction: .horizontal) {
  HStack {
    Text("One")
    Text("Two")
  }
}
D
ScrollView(.horizontal) {
  HStack {
    Text("One")
    Text("Two")
  }
}
Attempts:
2 left
💡 Hint
Check the official ScrollView initializer parameters.
🔧 Debug
expert
2:00remaining
ScrollView Content Not Scrolling
Why does this ScrollView not scroll even though the content is larger than the screen?
iOS Swift
ScrollView {
  VStack {
    ForEach(0..<20) { i in
      Text("Item \(i)").frame(height: 50)
    }
  }
}.frame(height: 200)
AThe ScrollView frame height is fixed, but the VStack does not expand beyond 200, so no scrolling occurs.
BThe ScrollView frame height is fixed, but the VStack content size is not larger than 200, so no scrolling.
CThe ScrollView frame height is fixed, but the VStack content is clipped, preventing scrolling.
DThe ScrollView frame height is fixed to 200, so it only shows that height and scrolls the rest.
Attempts:
2 left
💡 Hint
Check the total height of VStack content compared to ScrollView frame height.