簡體   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