gpt4 book ai didi

swiftui - Swift ScrollView 再次置顶

转载 作者:行者123 更新时间:2023-12-05 05:32:24 28 4
gpt4 key购买 nike

我有一个简单的屏幕,其中的内容位于底部,所以我在其上使用 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com