swiftui - Swift ScrollView 再次置顶

标签 swiftui

我有一个简单的屏幕,其中的内容位于底部,所以我在其上使用 ScrollView 问题是当我释放滚动时它再次回到顶部。我需要将 scrollview 替换为某些东西因为我尝试使用 List 但它不能与背景图像一起正常工作所以我使用 Scrollview

struct signupView: View {
    @StateObject var signUpViewModel = signupViewModel()
    
    var body: some View {
        ZStack{
            GeometryReader { geo in
                ScrollView {
                    VStack{
                        VStack{
                            Image("Untitled-2")
                                .resizable()
                                .frame(width: geo.size.width * 0.45, height: geo.size.width * 0.45)
                            
                        }
                        .padding(.top, geo.size.height * 0.03)
                        
                        
                        HStack(spacing: 0){
                            VStack{
                                Text("Student")
                                    .foregroundColor(signUpViewModel.isStudent ? Color("secondarycolor") : Color("authColorText"))
                                Rectangle()
                                    .fill(signUpViewModel.isStudent ? Color("secondarycolor") : Color("authColorText"))
                                    .frame(width: geo.size.width * 0.4, height: 2)
                                
                            }
                            .onTapGesture {
                                signUpViewModel.isStudent = true
                            }
                            
                            VStack{
                                Text("Facilitator")
                                    .foregroundColor(!signUpViewModel.isStudent ? Color("secondarycolor") : Color("authColorText"))
                                
                                Rectangle()
                                    .fill(!signUpViewModel.isStudent ? Color("secondarycolor") : Color("authColorText"))
                                    .frame(width: geo.size.width * 0.4, height: 2)
                                
                            }
                            .onTapGesture {
                                signUpViewModel.isStudent = false
                            }
                        }
                        
                        VStack{
                            VStack{
                                Spacer().frame(height: geo.size.height * 0.7)
                                
                                TextField("", text: $signUpViewModel.nameField)
                                
                                    .modifier(PlaceholderStyle(showPlaceHolder: signUpViewModel.nameField.isEmpty,
                                                               placeholder: "Name"))
                                
                                    .padding()
                                    .background(RoundedRectangle(cornerRadius: 50).fill(Color("primarycolorwithopacity")))
                                    .foregroundColor(Color("authColorText"))
                                    .foregroundColor(Color("authColorText")).font(Font.headline.weight(.medium))
                                    .padding(.bottom, 10)
                                
                                TextField("", text: $signUpViewModel.emailField)
                                
                                    .modifier(PlaceholderStyle(showPlaceHolder: signUpViewModel.emailField.isEmpty,
                                                               placeholder: "Email"))
                                
                                    .padding()
                                    .background(RoundedRectangle(cornerRadius: 50).fill(Color("primarycolorwithopacity")))
                                    .foregroundColor(Color("authColorText"))
                                    .foregroundColor(Color("authColorText")).font(Font.headline.weight(.medium))
                                    .padding(.bottom, 10)
                                
                                SecureField("", text: $signUpViewModel.passwordField)
                                
                                    .modifier(PlaceholderStyle(showPlaceHolder: signUpViewModel.passwordField.isEmpty,
                                                               placeholder: "Password"))
                                
                                    .padding()
                                    .background(RoundedRectangle(cornerRadius: 50).fill(Color("primarycolorwithopacity")))
                                    .foregroundColor(Color("authColorText"))
                                    .foregroundColor(Color("authColorText")).font(Font.headline.weight(.medium))
                                    .padding(.bottom, 10)
                                
                                SecureField("", text: $signUpViewModel.passwordRepeatField)
                                
                                    .modifier(PlaceholderStyle(showPlaceHolder: signUpViewModel.passwordRepeatField.isEmpty,
                                                               placeholder: "Repeat Password"))
                                
                                    .padding()
                                    .background(RoundedRectangle(cornerRadius: 50).fill(Color("primarycolorwithopacity")))
                                    .foregroundColor(Color("authColorText"))
                                    .foregroundColor(Color("authColorText")).font(Font.headline.weight(.medium))
                                    .padding(.bottom, 10)
                                
                                
                                TextField("", text: $signUpViewModel.countryField)
                                
                                    .modifier(PlaceholderStyle(showPlaceHolder: signUpViewModel.countryField.isEmpty,
                                                               placeholder: "Select Country"))
                                    .padding()
                                    .background(RoundedRectangle(cornerRadius: 50).fill(Color.black))
                                    .foregroundColor(Color("authColorText"))
                                    .foregroundColor(Color("authColorText")).font(Font.headline.weight(.medium))
                                    .padding(.bottom, 10)
                                    .overlay(
                                        
                                        ZStack{if signUpViewModel.hasOptions {
                                            Spacer().frame(height: 70)
                                            
                                            VStack(alignment: .center) {
                                                
                                                ForEach(signUpViewModel.countryoptions, id: \.self) {d in
                                                    Text(d).frame(maxWidth: .infinity).padding(8).onTapGesture {
                                                        signUpViewModel.countryField = d
                                                        signUpViewModel.hasOptions = false
                                                    }
                                                    Divider()
                                                }
                                                
                                            }.frame(maxWidth: .infinity).background(RoundedRectangle(cornerRadius: 6).foregroundColor(.white).shadow(radius: 4))
                                        }
                                            
                                            
                                            
                                        }.offset(y: 50)
                                        
                                        , alignment: .topLeading)
                                    .overlay(
                                        
                                        Image("Untitled-42")
                                            .resizable()
                                            .foregroundColor(.white)
                                            .frame(width: 15, height: 10).padding().padding(.bottom,2).onTapGesture {
                                                
                                                signUpViewModel.hasOptions = !signUpViewModel.hasOptions
                                                
                                            }
                                        
                                        , alignment: .trailing).zIndex(1)
                                
                                SecureField("", text: $signUpViewModel.passwordRepeatField)
                                
                                    .modifier(PlaceholderStyle(showPlaceHolder: signUpViewModel.passwordRepeatField.isEmpty,
                                                               placeholder: "Repeat Password"))
                                
                                    .padding()
                                    .background(RoundedRectangle(cornerRadius: 50).fill(Color("primarycolorwithopacity")))
                                    .foregroundColor(Color("authColorText"))
                                    .foregroundColor(Color("authColorText")).font(Font.headline.weight(.medium))
                                    .padding(.bottom, 10)
                                
                                
                            }.padding([.top, .leading, .trailing], 8).frame(height: 45)
                            
                            
                        }
                        
                    }
                    .padding()
                }
                
            }
            
        }.background(Image("Untitled-7").resizable()).edgesIgnoringSafeArea(.all)
    }
    
    
}

enter image description here

当我发布它回到顶部时。不明白为什么会发生这种情况。或者我需要使用其他东西

最佳答案

一种方法是使用 introspect 包,您可以在此处添加包 https://github.com/siteline/SwiftUI-Introspect.git

scrollView.bounces = false

以下是我使用 Introspect 的方法:

struct ContentView: View {
var body: some View {
    ScrollView {
        VStack {
            Image(systemName: "globe")
                .imageScale(.large)
                .foregroundColor(.accentColor)
            Text("Hello, world!")
        }
        .padding()
    }.introspectScrollView { scrollView in
        scrollView.bounces = false
    }
}

也不要忘记导入

import Introspect

关于swiftui - Swift ScrollView 再次置顶,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74132848/

相关文章:

ios - 有没有办法使用 SwiftUI 将可选对象绑定(bind)到 Toggle/Slider

ios - 对 SwiftUI FetchRequest 的更改未触发 View 刷新?

ios - SwiftUI @Binding 初始化

ios - 防止 NavigationLink 处理状态变量

ios - SwiftUI 中的 PreferredScreenEdgesDeferringSystemGestures

swift - 线程 1 : EXC_BAD_ACCESS (code=1, 地址=0x10) SwiftUI @main 应用程序

SwiftUI:如何在抚摸时使整个形状识别手势?

firebase - SwiftUI:从 DetailView 更新 Firestore

ios - SwiftUI MVVM : child view model re-initialized when parent view updated

ios - 使用 SwiftUI 添加谷歌登录