简体   繁体   English

中心滚动查看内容

[英]Center Scroll View content

I have centered my Scroll View by using GeometryReader like this:我使用 GeometryReader 将我的滚动视图居中,如下所示:

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())
    }
}

在此处输入图片说明

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:我的问题是容器 ZStack 的高度太大,页面可以像这样向下滚动: 在此处输入图片说明 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(). UPD:我发现我可以使用 position() 而不是 frame()。 In that case page centred properly, but it may cause some issues in future.在这种情况下,页面正确居中,但将来可能会导致一些问题。

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我不得不使用.inline NavigationBarDisplayMode 和position而不是设置 ZStack 的宽度和高度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM