【问题标题】:Why is SwiftUI not hiding my status bar on this fullScreenCover?为什么 SwiftUI 没有在这个 fullScreenCover 上隐藏我的状态栏?
【发布时间】:2020-12-10 12:00:40
【问题描述】:

我的代码如下。 .statusBar(hidden: true) 应该在不同的地方吗?它是否取决于我不知道的一些@Environment

import SwiftUI

struct ContentView: View {
    
    @State private var isShowingFullScreenModal = false
    
    var body: some View {
        Text("Show Modal")
            .font(.title)
            .foregroundColor(.white)
            .frame(width: 180, height: 44)
            .padding()
            .background(Color.blue)
            .cornerRadius(12)
            .onTapGesture {
                isShowingFullScreenModal = true
            }
            .fullScreenCover(isPresented: $isShowingFullScreenModal) {
                Color.green
                    .ignoresSafeArea(.all)
                    .onTapGesture {
                        isShowingFullScreenModal = false
                    }
            }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}


struct GreenCover: View {
    
    @Environment(\.presentationMode) var presentationMode
    
    var body: some View {
        
        ZStack {
            Color.green
                .ignoresSafeArea(.all)
            VStack{
                HStack{
                    Spacer()
                    Image(systemName: "xmark.circle.fill")
                }
                Spacer()
            }
        }
        .statusBar(hidden: true)
    }
    
}

【问题讨论】:

    标签: swiftui statusbar


    【解决方案1】:

    这是固定的变体。使用 Xcode 12.1 / iOS 14.1 测试

    .onTapGesture {
        isShowingFullScreenModal = true
    }
    .statusBar(hidden: isShowingFullScreenModal)                // << here !!
    .fullScreenCover(isPresented: $isShowingFullScreenModal) {
    

    【讨论】:

      【解决方案2】:

      有时在我们的应用程序中,我们需要隐藏状态栏、导航栏和其他东西,只显示我们想要显示的内容。要使用 swift 语言在我们的 iOS 应用程序中隐藏状态栏,我们需要执行非常基本的步骤。

      方法一

      在您的 info.plist 文件本身中,添加另一个名为 Status bar is initially hidden 的键并将其设置为 YES。

      我注意到这种方法是,状态栏只有在应用程序启动时才会隐藏。

      方法二

      转到您的应用委托文件,并添加以下代码行:UIApplication.shared.isStatusBarHidden = true

      这在 iOS 13 上已被弃用。

      方法3

      你也可以添加这行代码,状态栏会被隐藏。

      .edgesIgnoringSafeArea(.all)
      .statusBar(hidden: true)
      

      【讨论】:

      • 如果您想回答我的问题,我不确定这是否有帮助。这只是一个一般性的解释。我的原始代码确实有.statusBar(hidden: true)。如果在第 3 点下,您的意思是特别添加 .edgesIgnoringSafeArea(.all),那么明确地说出来会很有帮助。还是您的意思是必须添加两条线?以该顺序?我的代码在 Z 堆栈下确实有 .ignoresSafeArea(.all)。你是说ignores... 代码必须在status bar hidden... 代码之前?我真的很抱歉,但我根本不明白这如何回答我的问题。能说清楚一点吗?
      猜你喜欢
      • 2019-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多