【问题标题】:SwiftUI - ScrollView won't me scroll to the bottom because of minus values in paddingsSwiftUI - 由于填充中的负值,ScrollView 不会滚动到底部
【发布时间】:2020-05-27 00:38:17
【问题描述】:

我不知道滚动视图内容大小或偏移量有问题。

当我尝试滚动视图时,它会返回并停留在顶部,但正如您所见,我在屏幕底部有按钮,并且想要滚动到视图底部以查看整个 NEXT 按钮。

看起来内容大小有误:

这是我的代码https://github.com/matrosovDev/swiftui的回购

有一个 WelcomeView 组件,其中包含所有代码,但我也会将其复制粘贴在这里,可能使用负值填充或偏移会导致此滚动视图内容大小或偏移问题:

struct WelcomeView: View {

    @State private var password = ""
    @State var showWelcomeView = false
    @State var showActivityIndicator = false

    @EnvironmentObject var userService: UserService

    var body: some View {

        ZStack {
            Color.customLightGray
            if self.showActivityIndicator {
                VStack {
                    HStack {
                        Spacer()
                        LottieView(named: "19451-blue-preloader",
                                   loop: self.showActivityIndicator,
                                   size: 200
                        )
                        Spacer()
                    }
                }
            } else {

                ScrollView {

                    VStack {
                        Image("MountainWelcomBackground").resizable().frame(height: 300)

                        CircleImage(image: userService.user.image)
                            .padding(.top, -150)
                            .frame(height: 140)
                            .frame(width: 140)

                        VStack(alignment: .leading) {
                            HStack(alignment: .top) {
                                Spacer()
                                Text(userService.user.username)
                                    .font(.headline)
                                Spacer()
                            }
                        }
                        .padding(.top, -70)

                        VStack (alignment: .leading, spacing: 10) {

                            Text("Password:")
                                .font(.headline)

                            TextField("Enter your password", text: $password)
                                .padding(.all)
                                .font(Font.system(size: 18, weight: .medium, design: .rounded))
                                .overlay(RoundedRectangle(cornerRadius: 8).stroke(Color.customCorporateBlue, lineWidth: 1))
                                .foregroundColor(Color.customCorporateBlue)
                                .keyboardType(.emailAddress)
                                .autocapitalization(.none)

                            Button(action: {
                            }) {
                                Text("Forgot password?")
                                    .fontWeight(.bold)
                                    .font(.headline)
                                    .padding(EdgeInsets(top: 20, leading: 10, bottom: 20, trailing: 0))
                                    .foregroundColor(.customCorporateBlue)
                            }

                            HStack {
                                Button(action: {
                                }) {
                                    Text("Create account")
                                        .fontWeight(.bold)
                                        .font(.subheadline)
                                        .padding()
                                        .foregroundColor(.customCorporateBlue)
                                }

                                Spacer()

                                Button(action: {
                                    //self.showActivityIndicator.toggle()
                                    //self.fetchUser(with: self.email)
                                }) {
                                    Text("Next")
                                        .fontWeight(.bold)
                                        .font(.title)
                                        .padding(EdgeInsets(top: 20, leading: 40, bottom: 20, trailing: 40))
                                        .background(Color.customCorporateBlue)
                                        .cornerRadius(8)
                                        .foregroundColor(.white)
                                }

                                //NavigationLink(destination: WelcomeView(), isActive: $showWelcomeView) { EmptyView() }
                            }

                        }.padding(.horizontal, 30)
                            .modifier(AdaptsToKeyboard())
                            .padding(.top, -20)

                        Spacer()
                    }

                }.edgesIgnoringSafeArea(.top)
            }
        }
    }
}

【问题讨论】:

    标签: uiscrollview swiftui


    【解决方案1】:

    这是由于许多硬编码会影响不同手机的布局。对于这个特定的用例,我会推荐以下简单的解决方案。

    使用部分复制的代码进行测试(Xcode 11.4 / iOS 13.4 / iPhone8 / 11 Max)

    // make height of top image relative to available screen space
    VStack {
        Image("MountainWelcomBackground")
           .resizable()
           .frame(height: UIScreen.main.bounds.size.height / 3.0)
    

    【讨论】:

    • 我喜欢你使用 UIScreen (UIKit) 而不是几何阅读器(也可以使用)。提醒我旧方法仍然有效!
    • @Mozahler,没有老路……只有 :^)
    • 哇)谢谢!有效,但是如果我需要特定图像的确切高度大小怎么办。我知道你根据我的代码做了这个例子。我还找到了一种方法,通过添加 .padding(.bottom, 240) 为放置在 VStack 中的最底部视图添加一些额外的填充。不确定它是否好,但我不需要使用相对图像高度。无论如何,谢谢!)
    猜你喜欢
    • 1970-01-01
    • 2020-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-24
    • 1970-01-01
    • 2013-02-14
    • 1970-01-01
    相关资源
    最近更新 更多