Home > AI > IOS > SwiftUI >

ScrollView

ScrollView {
    VStack(spacing: 20) {
        ForEach(0..<10) {
            Text("Item \($0)")
            .foregroundColor(.white)
            .font(.largeTitle)
            .frame(width: 200, height: 200)
            .background(Color.red)
            }
        }
    }

Example 2: the relationship between ScrollView and VStack

Default it has a VStack, 1 is same as 2.

struct ContentView : View {

   
    var body: some View {
        
        // 1
        ScrollView {
            Text("1")
                .frame(width: UIScreen.main.bounds.width)
                .background(Color.blue)
            
            Text("2")
                .frame(width: UIScreen.main.bounds.width)
                .background(Color.orange)
            
            Text("3")
                .frame(width: UIScreen.main.bounds.width)
                .background(Color.red)
        }
        .background(Color.gray)
        
        // 2 (same as 1)
        ScrollView {
            VStack {
                Text("1")
                    .frame(width: UIScreen.main.bounds.width)
                    .background(Color.blue)
                
                Text("2")
                    .frame(width: UIScreen.main.bounds.width)
                    .background(Color.orange)
                
                Text("3")
                    .frame(width: UIScreen.main.bounds.width)
                    .background(Color.red)
            }
        }
        .background(Color.gray)
        
        // 3
        ScrollView {
            VStack {
                Rectangle()
                    .foregroundColor(.green)
                    .frame(height:16)
                
                Rectangle()
                    .foregroundColor(.blue)
                    .frame(height:16)
            }
        }
        .frame(width: UIScreen.main.bounds.width)
        .background(Color.gray)
        
    }

}

Leave a Reply