NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

How to make dynamic ScrollView?

Forums > SwiftUI

Good day. Tell me how to make a dynamic ScrollView? If you add .fixedSize (horizontal: false, vertical: true), then with a large list of scrollViews it will not scroll.

(https://joxi.ru/a2XYoRju4oqKnA)

struct SearchView: View {

    @State var searchText = ""

    var body: some View {
        GeometryReader { geometry in
            VStack {
                LinearGradient.customVertical.frame(height: geometry.safeAreaInsets.top + 200)
                VStack {
                    contentBlock
                }
                .padding(.top, -100)
            }
        }
        .background(Color.gray)
        .edgesIgnoringSafeArea(.all)
    }

    var contentBlock: some View {
        GeometryReader { geometry in
            VStack {
                searchBlock
                scrollBlock
            }
            .background(Color.white)
            .cornerRadius(13)
            .padding(.horizontal, 15)
        }
    }

    var scrollBlock: some View {
        ScrollView(showsIndicators: true) {
            VStack {
                ForEach(1..<10, id: \.self) { elem in
                    labelBlock(elem: "element - \(elem)")
                }
            }
        }
        .border(Color.green, width: 4)
        .fixedSize(horizontal: false, vertical: true) 
    }

    func labelBlock(elem: String) -> some View {
        VStack(alignment: .leading) {
            Text(elem)
                .font(.system(size: 16))
                .foregroundColor(Color.blue)
                .padding(.top, 18)
            Text("Text")
                .font(.system(size: 16))
                .foregroundColor(Color.gray)
                .padding(.top, 10)
            Rectangle()
                .fill(Color.black)
                .frame(height: 1)
        }
        .padding(.leading, 31)
    }

    var searchBlock: some View {
        HStack {
            ZStack {
                if searchText.isEmpty {
                    Text("Input text")
                        .font(.system(size: 14, weight: .regular))
                }
                TextField("", text: $searchText)
            }
            Spacer()
            Button(action: {
                searchText = ""
            } ) {
                Image(systemName: "xmark")
                    .foregroundColor(.blue)
                    .font(.system(size: 17, weight: .semibold))
            }
        }
        .padding(.horizontal, 15)
        .frame(height: 68)
        .overlay(RoundedRectangle(cornerRadius: 13)
                    .stroke(Color.blue))
        .padding(15)
    }

}

struct SearchView_Previews: PreviewProvider {
    static var previews: some View {
        SearchView()
    }
}

extension LinearGradient {
    static let gradientBlue = LinearGradient(gradient: Gradient(colors: [Color(UIColor.systemBlue), Color.blue]), startPoint: .top, endPoint: .bottom)
}

1      

Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.