简体   繁体   English

如何让SwiftUI全屏显示?

[英]How to make SwiftUI view fullscreen?

As the title already says I'm trying to make a view fullscreen (make it extend over the SafeArea ), but SwiftUI seems to always align views to the safeArea .正如标题所说,我正在尝试全屏查看(使其延伸到SafeArea ),但SwiftUI似乎总是将视图与safeArea对齐。

After researching this for a while I found .edgesIgnoringSafeArea(.all) which seems like a pretty straightforward way to do it.对此进行了一段时间的研究后,我发现.edgesIgnoringSafeArea(.all)似乎是一种非常简单的方法。 The problem is that it doesn't work.问题是它不起作用。 The view still isn't full screen.视图仍然不是全屏。 Here is some example code:这是一些示例代码:

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

Just swap the ..background(Color.red) and .edgesIgnoringSafeArea(.all).只需交换 ..background(Color.red) 和 .edgesIgnoringSafeArea(.all)。 And it will work perfectly.它会完美地工作。

struct ContentView : View {

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

    }
}

The Augmented Reality App with SwiftUI also has a problem in entering full screen.带有 SwiftUI 的增强现实应用程序也存在进入全屏的问题。 (Xcode 12.3, SwiftUI 5.3) (Xcode 12.3,SwiftUI 5.3)

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

The code above is from the AR template.上面的代码来自 AR 模板。 However, it doesn't work.但是,它不起作用。

The solution is tricky: you have to set "LaunchScreen.storyboard" as "Launch Screen File" in "[yourTarget] -> General -> App Icons and Launch Images".解决方案很棘手:您必须在“[yourTarget] -> General -> App Icons and Launch Images”中将“LaunchScreen.storyboard”设置为“Launch Screen File”。 If there is no "LaunchScreen.storyboard" in the project, it can still work by entering "LaunchScreen" in that field.如果项目中没有“LaunchScreen.storyboard”,它仍然可以通过在该字段中输入“LaunchScreen”来工作。

I found a similar discussion on Apple forum that explains the potential reason of failure:我在 Apple 论坛上发现了一个类似的讨论,解释了失败的潜在原因:

If that isn't set properly, then it sounds like your app is launching into a compatibility mode which is letterboxing your app.如果设置不正确,那么听起来您的应用程序正在启动兼容模式,该模式将您的应用程序装箱。

According to Apple documentation from iOS 14 you can use fullScreenCover(item:onDismiss:content:)根据 iOS 14 的 Apple 文档,您可以使用 fullScreenCover(item:onDismiss:content:)

And here is sample code:这是示例代码:

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