繁体   English   中英

如何让SwiftUI全屏显示?

[英]How to make SwiftUI view fullscreen?

正如标题所说,我正在尝试全屏查看(使其延伸到SafeArea ),但SwiftUI似乎总是将视图与safeArea对齐。

对此进行了一段时间的研究后,我发现.edgesIgnoringSafeArea(.all)似乎是一种非常简单的方法。 问题是它不起作用。 视图仍然不是全屏。 这是一些示例代码:

struct ContentView : View {
    var body: some View {
        Text("Test")
            .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
            .edgesIgnoringSafeArea(.all)
            .background(Color.red)
    }
}

只需交换 ..background(Color.red) 和 .edgesIgnoringSafeArea(.all)。 它会完美地工作。

struct ContentView : View {

    var body: some View {
        Text("Test")
            .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
            .background(Color.red)
            .edgesIgnoringSafeArea(.all)

    }
}

带有 SwiftUI 的增强现实应用程序也存在进入全屏的问题。 (Xcode 12.3,SwiftUI 5.3)

struct ContentView : View {
    var body: some View {
        return ARViewContainer().edgesIgnoringSafeArea(.all)
    }
}

上面的代码来自 AR 模板。 但是,它不起作用。

解决方案很棘手:您必须在“[yourTarget] -> General -> App Icons and Launch Images”中将“LaunchScreen.storyboard”设置为“Launch Screen File”。 如果项目中没有“LaunchScreen.storyboard”,它仍然可以通过在该字段中输入“LaunchScreen”来工作。

我在 Apple 论坛上发现了一个类似的讨论,解释了失败的潜在原因:

如果设置不正确,那么听起来您的应用程序正在启动兼容模式,该模式将您的应用程序装箱。

根据 iOS 14 的 Apple 文档,您可以使用 fullScreenCover(item:onDismiss:content:)

这是示例代码:

struct ContentView: View {
    @State private var isFullScreen = false
    var body: some View {
        ZStack{
        Color.yellow.edgesIgnoringSafeArea(.all)
        Text("Hello, FullScreen!")
            .padding()
            .background(Color.blue)
            .foregroundColor(.green)
            .cornerRadius(8)
            .fullScreenCover(isPresented: $isFullScreen) {
                FullScreen(isFullScreen: $isFullScreen)
            }
            .onTapGesture {
                isFullScreen.toggle()
            }
    }
    }
}

struct FullScreen: View {
    @Binding var isFullScreen: Bool
    
    var body: some View {
        ZStack {
            Color.red.edgesIgnoringSafeArea(.all)
            Text("This is full screen!!")
                .onTapGesture {
                    self.isFullScreen.toggle()
                }
            
        }
    }
}
ZStack {
                    Text("Test")
                    Rectangle()
                        .fill(.clear)
                        .background(Color.clear)
                        .edgesIgnoringSafeArea(.all)
                        .contentShape(Rectangle())
                        .onTapGesture {
                            //tap action
                        }
                }

暂无
暂无

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

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