【问题标题】:How to hide the status bar in SwiftUI如何在 SwiftUI 中隐藏状态栏
【发布时间】:2019-11-15 17:07:55
【问题描述】:

我想在 SwiftUI 中隐藏状态栏。我尝试了“statusBar(hidden:true)”方法,但它不起作用。是否有任何解决方案可以在 SwiftUI 中实现。

演示代码如下:

var body: some View {
        VStack {
            Text("Hello World")

        }
        .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
        .background(Color.blue)
        .edgesIgnoringSafeArea(.all)
        .statusBar(hidden: true)

    }

【问题讨论】:

    标签: hide statusbar swiftui


    【解决方案1】:
    .edgesIgnoringSafeArea(.all)
    .statusBar(hidden: true)
    

    试试这个

    【讨论】:

    • 如果使用 NavigationView,我发现在顶级 NavigationView 块上设置它效果最好。您无需在较低级别的屏幕上重新声明。
    • 很好的答案,有趣的是没有.edgesIgnoringSafeArea(.all) 它不会隐藏
    • 这似乎在 iOS 14 中被破坏了......无论我做什么,我都无法隐藏 statusBar 以在具有导航视图的屏幕上工作
    【解决方案2】:

    根据您的意图,您可以通过几种不同的方式在 SwiftUI 项目中隐藏状态栏。我列出了一些,没有特别的顺序。除 OPTION-2 外,所有示例均适用于使用 Xcode 11 和 Xcode 12 的 iOS 13 和 iOS 14。

    OPTION-1
    在你的 info.plist 文件中确保你有这些设置:

    <key>UIStatusBarHidden</key>
    <true/>
    <key>UIViewControllerBasedStatusBarAppearance</key>
    <false/>
    

    添加以下 UIViewController 扩展

    extension UIViewController {
        func prefersStatusBarHidden() -> Bool {
            return true
        }
    }
    

    OPTION-2(仅限 iOS 14 和 Xcode 12)
    在你的 info.plist 文件中确保你有这些设置:

    <key>UIStatusBarHidden</key>
    <true/>
    <key>UIViewControllerBasedStatusBarAppearance</key>
    <false/>
    

    按照此链接中的说明将 AppDelegate 添加到 SwiftUI 应用程序,但添加下面提到的行。 https://www.hackingwithswift.com/quick-start/swiftui/how-to-add-an-appdelegate-to-a-swiftui-app

    class AppDelegate: NSObject, UIApplicationDelegate {
        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
            UIApplication.shared.isStatusBarHidden = true // <== ADD THIS LINE
            return true
        }
    }
    

    请注意,您会收到已弃用的调用警告,但它应该仍然有效。

    OPTION-3
    在你的 info.plist 文件中确保你有这些设置:

    <key>UIStatusBarHidden</key>
    <true/>
    <key>UIViewControllerBasedStatusBarAppearance</key>
    <false/>
    

    确保您的初始 SwiftUI 视图是一个导航视图,您可以在其中隐藏状态栏。然后,如果您导航到选项卡栏视图或任何后续视图,状态栏将被隐藏。

    struct ContentView: View {
        var body: some View {
            NavigationView {
                NavigationLink(destination: MyTabView()) {
                    Text("Go To Tabview")
                }
            }
            .edgesIgnoringSafeArea(.all)
            .statusBar(hidden: true)
        }
    }
    
    struct MyTabView: View {
        var body: some View {
            TabView {
                TabView1().tabItem {
                    Text("Tab 1")
                }
    
                TabView2().tabItem {
                    Text("Tab 2")
                }
            }
        }
    }
    
    struct TabView1: View {
        var body: some View {
            Text("Tab View 1")
        }
    }
    
    struct TabView2: View {
        var body: some View {
            Text("Tab View 2")
        }
    }
    

    OPTION-4
    使用 UIStatusBarManager https://developer.apple.com/documentation/uikit/uistatusbarmanager

    【讨论】:

    • 已确认 OPTION-3 对我很有用。在 SwiftUI 生命周期应用程序上。 Xcode 12 + iOS 14
    • 你不能用 UIStatusBarManager 更新状态栏,isStatusBarHidden 只是 getter。
    【解决方案3】:

    iOS 14,SwiftUI

    我发现,iOS14 中最有效的方法是进入info.plist。但是,这种方法一直有效,也就是说,如果您想在点击 UIButton 时返回 statusBar,那么这种方法不适合您。

    对我来说,它适用于整个应用程序,无论是否使用 NavigationView 的实例。

    打开您的 Info.plist “作为源代码”(见下图)并将这四行粘贴到其中:

    <key>UIStatusBarHidden</key>
    <true/>
    <key>UIViewControllerBasedStatusBarAppearance</key>
    <false/>
    

    【讨论】:

      【解决方案4】:

      Xcode 13.0 beta 3 终于解决了这个问题 - 您可以将 statusBarHidden 修饰符附加到 TabView 和 NavigationView 并且状态栏会按预期隐藏。

      【讨论】:

        【解决方案5】:

        我发现让每个视图都能正常工作的唯一方法是将你的 rootView 包装在 HostingController 中,例如:

        class HostingController<ContentView>: UIHostingController<ContentView> where ContentView : View {
        
            
        
            init(view: ContentView) {
        
                super.init(rootView: view)
        
        
        
                NotificationCenter.default.addObserver(forName: .onDarkStatusBar, object: nil, queue: .main) { _ in
        
                    self.statusBarEnterDarkBackground()
        
                }
        
                
        
                NotificationCenter.default.addObserver(forName: .onLightStatusBar, object: nil, queue: .main) { _ in
        
                    self.statusBarEnterLightBackground()
        
                }
        
                
        
                NotificationCenter.default.addObserver(forName: .onShowStatusBar, object: nil, queue: .main) { _ in
        
                    self.statusBarShow()
        
                }
        
                
        
                NotificationCenter.default.addObserver(forName: .onHideStatusBar, object: nil, queue: .main) { _ in
        
                    self.statusBarHide()
        
                }
        
            }
        
            
        
            @objc required dynamic init?(coder aDecoder: NSCoder) {
        
                fatalError("init(coder:) has not been implemented")
        
            }
        
            
        
            private var isDarkContentBackground = false
        
            private var isStatusBarHiden = false
        
        
        
            func statusBarEnterDarkBackground() {
        
                isDarkContentBackground = false
        
                setNeedsStatusBarAppearanceUpdate()
        
            }
        
        
        
            func statusBarEnterLightBackground() {
        
                isDarkContentBackground = true
        
                setNeedsStatusBarAppearanceUpdate()
        
            }
        
            
        
            func statusBarHide() {
        
                isStatusBarHiden = true
        
                setNeedsStatusBarAppearanceUpdate()
        
            }
        
        
        
            func statusBarShow() {
        
                isStatusBarHiden = false
        
                setNeedsStatusBarAppearanceUpdate()
        
            }
        
        
        
            override var preferredStatusBarStyle: UIStatusBarStyle {
        
                if isDarkContentBackground {
        
                    return .lightContent
        
                }
        
                else
        
                {
        
                    return .darkContent
        
                }
        
            }
        
            
        
            override var prefersStatusBarHidden: Bool {
        
              return isStatusBarHiden
        
            }
        
        
        
        }
        

        用作

        let window = UIWindow(windowScene: windowScene)
        
                    window.rootViewController = HostingController(view: contentView)
        

        然后从 onDissapear 和 onAppear 发出通知。

        extension Notification.Name {
        
           static let onDarkStatusBar = Notification.Name("onDarkStatusBar")
        
            static let onLightStatusBar = Notification.Name("onLightStatusBar")
        
            static let onHideStatusBar = Notification.Name("onHideStatusBar")
        
            static let onShowStatusBar = Notification.Name("onShowStatusBar")
        
        }
        

        使用

                                    .onAppear {
        
                                        NotificationCenter.default.post(name: Notification.Name.onHideStatusBar, object: nil)
        
                                    }
        
                                    .onDisappear {
        
                                        NotificationCenter.default.post(name: Notification.Name.onShowStatusBar, object: nil)
        
                                    }
        

        【讨论】:

          猜你喜欢
          • 2022-06-20
          • 2016-02-15
          • 1970-01-01
          • 1970-01-01
          • 2011-07-22
          • 2012-09-21
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多