prefersHomeIndicatorAutoHidden

prefersHomeIndicatorAutoHidden

本文介绍了如何使用SwiftUI隐藏主页指示器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SwiftUI中prefersHomeIndicatorAutoHidden属性的UIKit等效项是什么?

What's the UIKit equivalent of the prefersHomeIndicatorAutoHidden property in SwiftUI?

推荐答案

由于我也无法在默认API中找到它,因此我自己在UIHostingController的子类中创建了它.

Since I could't find this in the default API either, I made it myself in a subclass of UIHostingController.

我想要的是:

var body: some View {
    Text("I hide my home indicator")
        .prefersHomeIndicatorAutoHidden(true)
}

由于prefersHomeIndicatorAutoHidden是UIViewController的属性,我们可以在UIHostingController中覆盖它,但是我们需要从视图中将prefersHomeIndicatorAutoHidden设置为视图层次结构,并将其设置为UIHostingController中的rootView.

Since the prefersHomeIndicatorAutoHidden is a property on UIViewController we can override that in UIHostingController but we need to get the prefersHomeIndicatorAutoHidden setting up the view hierarchy, from our view that we set it on to the rootView in UIHostingController.

在SwiftUI中我们执行此操作的方法是PreferenceKeys.网上有很多很好的解释.

The way that we do that in SwiftUI is PreferenceKeys. There is lots of good explanation on that online.

所以我们需要一个PreferenceKey来将值发送到UIHostingController:

So what we need is a PreferenceKey to send the value up to the UIHostingController:

struct PrefersHomeIndicatorAutoHiddenPreferenceKey: PreferenceKey {
    typealias Value = Bool

    static var defaultValue: Value = false

    static func reduce(value: inout Value, nextValue: () -> Value) {
        value = nextValue() || value
    }
}

extension View {
    // Controls the application's preferred home indicator auto-hiding when this view is shown.
    func prefersHomeIndicatorAutoHidden(_ value: Bool) -> some View {
        preference(key: PrefersHomeIndicatorAutoHiddenPreferenceKey.self, value: value)
    }
}

现在,如果我们在视图上添加.prefersHomeIndicatorAutoHidden(true),它将在视图层次结构中发送PrefersHomeIndicatorAutoHiddenPreferenceKey.为了在托管控制器中了解到这一点,我制作了一个子类,该子类包装了rootView来侦听首选项更改,然后更新UIViewController.prefersHomeIndicatorAutoHidden:

Now if we add .prefersHomeIndicatorAutoHidden(true) on a View it sends the PrefersHomeIndicatorAutoHiddenPreferenceKey up the view hierarchy. To catch that in the hosting controller I made a subclass that wraps the rootView to listen to the preference change, then update the UIViewController.prefersHomeIndicatorAutoHidden:

// Not sure if it's bad that I cast to AnyView but I don't know how to do this with generics
class PreferenceUIHostingController: UIHostingController<AnyView> {
    init<V: View>(wrappedView: V) {
        let box = Box()
        super.init(rootView: AnyView(wrappedView
            .onPreferenceChange(PrefersHomeIndicatorAutoHiddenPreferenceKey.self) {
                box.value?._prefersHomeIndicatorAutoHidden = $0
            }
        ))
        box.value = self
    }

    @objc required dynamic init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    private class Box {
        weak var value: PreferenceUIHostingController?
        init() {}
    }

    // MARK: Prefers Home Indicator Auto Hidden

    private var _prefersHomeIndicatorAutoHidden = false {
        didSet { setNeedsUpdateOfHomeIndicatorAutoHidden() }
    }
    override var prefersHomeIndicatorAutoHidden: Bool {
        _prefersHomeIndicatorAutoHidden
    }
}

没有公开PreferenceKey类型并且在git上也具有preferredScreenEdgesDeferringSystemGestures的完整示例: https://gist.github.com/Amzd/01e1f69ecbc4c82c8586dcd292b1d30d

Full example that doesn't expose the PreferenceKey type and has preferredScreenEdgesDeferringSystemGestures too on git: https://gist.github.com/Amzd/01e1f69ecbc4c82c8586dcd292b1d30d

这篇关于如何使用SwiftUI隐藏主页指示器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-03 06:29