1

I have centered my Scroll View by using GeometryReader like this:

struct ContentView: View {
    var body: some View {
        NavigationView {
            GeometryReader { geom in
                ScrollView(.vertical, showsIndicators: false) {
                    ZStack { 
                        my_super_view()
                    }
                    .navigationBarTitle("Main")
                    .frame(width: geom.size.width)
                    .frame(minHeight: geom.size.height)
                }
            }
        }.navigationViewStyle(StackNavigationViewStyle())
    }
}

enter image description here

And it works almost great. My problem is that the height of container ZStack is too big and the page could be scrolled down like this: enter image description here And when i try to do smth like:

.frame(minHeight: geom.size.height-52)

The page stops scrolling, but is poorly centered. How can I properly center the page so that it fits entirely on the main screen without scrolling below it?

UPD: I found out that i could use position() instead of frame(). In that case page centred properly, but it may cause some issues in future.

1
  • What's the reason to have scroll view if content is less than screen and you need to center content, scroll view is just not appropriate in this scenario. Commented Oct 24, 2021 at 14:41

1 Answer 1

0
struct ContentView: View {
    var body: some View {
        NavigationView {
            GeometryReader { geom in
                ScrollView(.vertical, showsIndicators: false) {
                    ZStack { 
                        my_super_view()
                    }
                    .navigationBarTitle("Main")
                    .position(x: geom.size.width/2, y: geom.size.height/2)
                }
            }
            .navigationBarTitle("Диа Компаньон")
            .navigationBarTitleDisplayMode(.inline)
        }.navigationViewStyle(StackNavigationViewStyle())
    }
}

I had to use .inline NavigationBarDisplayMode and position instead of setting up width and height of ZStack

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.