在 SwiftUI 中隐藏导航栏而不会丢失向后滑动手势 [英] Hide navigation bar without losing swipe back gesture in SwiftUI

查看:47
本文介绍了在 SwiftUI 中隐藏导航栏而不会丢失向后滑动手势的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 SwiftUI 中,每当隐藏导航栏时,滑动返回手势也会被禁用.

In SwiftUI, whenever the navigation bar is hidden, the swipe to go back gesture is disabled as well.

有什么办法可以隐藏导航栏,同时保留 SwiftUI 中的向后滑动手势?我已经有一个自定义的返回"按钮,但仍然需要手势.

Is there any way to hide the navigation bar while preserving the swipe back gesture in SwiftUI? I've already had a custom "Back" button, but still need the gesture.

我已经看到了一些 UIKit 的解决方案,但仍然不知道如何在 SwiftUI 中实现

I've seen some solutions for UIKit, but still don't know how to do it in SwiftUI

这是自己尝试的代码:

import SwiftUI

struct RootView: View {
    var body: some View {
        NavigationView {
            NavigationLink(destination: SecondView()) {
                Text("Go to second view")
            }
        }
    }
}

struct SecondView: View {
    var body: some View{
        Text("As you can see, swipe to go back will not work")
        .navigationBarTitle("")
        .navigationBarHidden(true)
    }
}

非常感谢任何建议或解决方案

Any suggestions or solutions are greatly appreciated

推荐答案

这比 Nick Bellucci 的回答还要简单.这是最简单的工作解决方案:

It is even easier than what Nick Bellucci answered. Here is the simplest working solution:

extension UINavigationController {
    override open func viewDidLoad() {
        super.viewDidLoad()
        interactivePopGestureRecognizer?.delegate = nil
    }
}

这篇关于在 SwiftUI 中隐藏导航栏而不会丢失向后滑动手势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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