ios .edgesIgnoringSafeArea(.top)影响宽度并导致页面内容缩小

nfs0ujit  于 2023-05-19  发布在  iOS
关注(0)|答案(1)|浏览(122)

我正在创建一个像TikTok一样的全页垂直滚动。
我可以使用以下代码来实现:

struct ViewA: View {
    let colors: [Color] = [ .red, .green, .blue, .gray ]

    var body: some View {
        GeometryReader { proxy in
            TabView {
                ForEach(colors, id: \.self) { color in
                    ZStack {
                        color
                        VStack {
                            Text("Hello World")
                        }
                    }
                }
                .rotationEffect(.degrees(-90))
                .frame(
                    width: proxy.size.width,
                    height: proxy.size.height
                )
            }
            .frame(
                width: proxy.size.height,
                height: proxy.size.width
            )
            .rotationEffect(.degrees(90), anchor: .topLeading)
            .offset(x: proxy.size.width)
            .tabViewStyle(
                PageTabViewStyle(indexDisplayMode: .never)
            )
        }
    }
}

但是,当我使用.edgesIgnoringSafeArea(.top)删除顶部的安全区域(使内容填充整个屏幕,包括安全区域)时,我的内容的宽度缩小了一点。

var body: some View {
    GeometryReader { proxy in
        TabView {
            ForEach(colors, id: \.self) { color in
                ZStack {
                    color
                    VStack {
                        Text("Hello World")
                    }
                }
            }
            .rotationEffect(.degrees(-90))
            .frame(
                width: proxy.size.width,
                height: proxy.size.height
            )
        }
        .frame(
            width: proxy.size.height,
            height: proxy.size.width
        )
        .rotationEffect(.degrees(90), anchor: .topLeading)
        .offset(x: proxy.size.width)
        .tabViewStyle(
            PageTabViewStyle(indexDisplayMode: .never)
        )
    }
    .edgesIgnoringSafeArea(.top) <----- This line causes the problem
}

我对原生ios开发非常陌生,请帮助。TIA~!

vngu2lb8

vngu2lb81#

您可以尝试使用以下代码,其中包括一个.edgesIgnoringSafeArea(.vertical)行和一些代码行的注解。
希望能帮到你。

struct ViewA: View {
    let colors: [Color] = [ .red, .green, .blue, .gray ]

    var body: some View {
//        GeometryReader { proxy in
            TabView {
                ForEach(colors, id: \.self) { color in
                    ZStack {
                        color
                            .edgesIgnoringSafeArea(.vertical) //Added here
                        VStack {
                            Text("Hello World")
                        }
                    }
                }
//                .rotationEffect(.degrees(-90))
//                .frame(
//                    width: proxy.size.width,
//                    height: proxy.size.height
//                )
            }
//            .frame(
//                width: proxy.size.height,
//                height: proxy.size.width
//            )
//            .rotationEffect(.degrees(90), anchor: .topLeading)
//            .offset(x: proxy.size.width)
//            .tabViewStyle(
//                PageTabViewStyle(indexDisplayMode: .never)
//            )
//        }
    }
}

结果:

相关问题