swiftswiftuibackgroundscrollview

SwiftUI: Can you have a different background color for the top and bottom of a ScrollView?


Is it possible to have different background color for the top and bottom of a ScrollView?

For example, I when I scroll upwards, I'd like the green background of the ScrollView to be blue, and when I scroll downwards I'd like the green background of the ScrollView to be red:

enter image description here


Solution

  • Yes, replace your background with this:

    .background(
        VStack(spacing: .zero) { 
            Color.blue; 
            Color.red 
        }
        .ignoresSafeArea()
    )