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)
}
