NuPlay / FitScrollView

Scrollview that fits the size of the child view

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

FitScrollView

Swift: 5.1+ iOS: 13.0+

githubFit scrollview
FitScrollView ScrollView(SwiftUI)

It is the same as SwiftUI's ScrollView, but for ScrollView, the height (vertical) or width (horizontal) is maximized.
Prevent it and create a ScrollView that fits the size of the internal view.
However, if the size of the internal view is large enough to require scrolling, it will be the same as the normal Scroll View.

Code

import SwiftUI
import FitScrollView

struct FitScrollView_Test: View {
    @State var  html = ""
    
    var body: some View {
       FitScrollView{
            Text("")
        }
    }
}
import SwiftUI
import FitScrollView

struct FitScrollView_Test: View {
    @State var  html = ""
    
    var body: some View {
        FitScrollView(.vertical, showsIndicators: false, content: {
            Text("")
        }
    }
}

SampleCode

import SwiftUI
import FitScrollView

struct FitScrollViewTest: View {
    @State var randomText : [String] = ["Cat","Apple", "pepper", "zero"]
    
    var body: some View {
        ZStack(alignment: .topTrailing){
            ZStack(alignment: .bottom){
                Color.black.opacity(0.4)
                    .ignoresSafeArea()
                
                FitScrollView(.vertical, showsIndicators: false, content: {
                    VStack(alignment: .leading,spacing: 12){
                        ForEach(0 ..< randomText.count , id:\.self) { index in
                            HStack{
                                Text(randomText[index])
                                    .font(.body)
                                    .padding(.horizontal, 12)
                                
                                Spacer()
                            }
                        }
                    }
                })
                .padding(.all, 12)
                .background(
                    Color.white
                        .cornerRadius(16)
                        .ignoresSafeArea()
                )
            }
            
            Button(action: {
                self.randomText.append(contentsOf: ["Cat","Apple", "pepper", "zero"])
            }, label: {
                Text("Add")
            })
            .padding(.horizontal, 12)
            .padding(.vertical, 4)
            .background(
                Capsule()
                    .foregroundColor(.white)
                    .opacity(0.7)
            )
            .padding(.trailing, 16)
        }
        .animation(.spring())
    }
}

struct FitScrollViewTest_Previews: PreviewProvider {
    static var previews: some View {
        FitScrollViewTest()
    }
}

About

Scrollview that fits the size of the child view

License:MIT License


Languages

Language:Swift 100.0%