lmh
lmh

Reputation: 323

SwiftUI View automatically scrolls to bottom without input

I am trying to wrap my entire View (itself wrapped in a VStack) into a ScrollView. When I run the app in simulator (for various different devices) or physical device, as soon as the page loads, the view automatically scrolls to the bottom in two staggered movements (i.e. not smoothly). Of course, I want the user to see the top of the page first and be able to scroll down themselves.

I'm just using a basic ScrollView { } with no modifiers or inputs.

Is there something I need to add to the ScrollView to prevent this, or could this be caused by something else in my View?

EDIT 2: I found the answer (posted below), so I've edited my code down here so you can see what the problem was and how I fixed it.

import SwiftUI

struct DetailViewTest: View {

    var body: some View {

        ScrollView {

            VStack {

                [Content Removed for Clarity]

            } //End of main VStack

                .navigationBarTitle(Text(""), displayMode: .inline)
                .padding()

        } //End of Scroll View

    }
}

Upvotes: 3

Views: 1082

Answers (1)

lmh
lmh

Reputation: 323

SOLVED: I moved the .navigationBarTitle and .padding() outside of the ScrollView curly braces so they are modifying the ScrollView instead of the main VStack:

        ...

        } //End of main VStack

            .frame(minWidth: 0, maxWidth: .infinity)

                } // End scroll view

            .navigationBarTitle(Text(""), displayMode: .inline)
            .padding()

    }
}

This must have just been a quirk with how SwiftUI is rendering the view and applying modifiers.

Note: I also added the .frame(minWidth: 0, maxWidth: .infinity) to my VStack which I don't think was related to my problem, though you may find it helpful in making ScrollView behave properly.

Upvotes: 2

Related Questions