在 Swift 2.0 中隐藏主页指示器 [英] Hide the Home Indicator in Swift 2.0

查看:23
本文介绍了在 Swift 2.0 中隐藏主页指示器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望隐藏 Home Indicator,虽然这在 Swift 中很简单,但在 SwiftUI 中似乎并不那么容易.

I'm looking to hide the Home Indicator and while this is straightforward in Swift doesn't appear to be as easy in SwiftUI.

我尝试使用这个:

如何使用 SwiftUI 隐藏主页指示器?

但是,随着 SceneDelegate 的删除,我不知道如何为新的应用协议正确地转换它.

But with the removal of the SceneDelegate I'm too green to know how to properly translate that for the new app protocol.

有人有什么想法吗?

推荐答案

这里有一种可能的方法来替换默认的 WindowGroup 窗口的宿主控制器与任何自定义控制器(在这种情况下没有主页指示器).

Here is possible approach to replace default WindowGroup window's hosting controller with any custom one (in this case w/o home indicator).

辅助扩展取自之前在 https://stackoverflow.com/a/63276688/12299030 中提供的解决方案.

The helper extension are taken from before provided solution in https://stackoverflow.com/a/63276688/12299030.

使用 Xcode 12/iOS 14 测试

Tested with Xcode 12 / iOS 14

@main
struct MyApp: App {
    @UIApplicationDelegateAdaptor(MyAppDelegate.self) var appDelegate

    var body: some Scene {
        WindowGroup {
            Text("")  // << temporary placeholder
                .withHostingWindow { window in
                    let contentView = ContentView().environmentObject(SomeObservableObject())
                    window?.rootViewController = 
                         HideHomeIndicatorController(rootView: contentView)
                }
        }
    }
}

以及用于隐藏主页指示器的托管控制器的简化变体

and simplified variant of hosting controller to hide home indicator

class HideHomeIndicatorController<Content:View>: UIHostingController<Content> {
    override var prefersHomeIndicatorAutoHidden: Bool {
        true
    }
}

这篇关于在 Swift 2.0 中隐藏主页指示器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆