SwiftUI-自定义导航栏后退按钮禁用滑动手势

SwiftUI-自定义导航栏后退按钮禁用滑动手势,swiftui,Swiftui,我在SwiftUI中创建了自己的后退按钮,而没有使用导航栏。一切都很顺利。但是当我从第一个视图切换到第二个视图时,我无法向后滑动。我与你分享了我的代码,我的雪崩为你的解决方案建议 第一视图: struct LogInView: View { @State var showSignUpScreen = false var body: some View { NavigationView{ NavigationLink( destination: Sign

我在SwiftUI中创建了自己的后退按钮,而没有使用导航栏。一切都很顺利。但是当我从第一个视图切换到第二个视图时,我无法向后滑动。我与你分享了我的代码,我的雪崩为你的解决方案建议

第一视图:

struct LogInView: View {
  @State var showSignUpScreen = false
  var body: some View {
    NavigationView{
      NavigationLink(
        destination: SignUpView(),
        isActive: self.$showSignUpScreen,
        label: {
        Text("")
        })

      Button(action: {
        self.showSignUpScreen.toggle()
        }, label: {
        Text("Sign Up")
        .fontWeight(.semibold)
        .foregroundColor(.blue)
        .frame(width: UIScreen.main.bounds.width * 0.3, height: UIScreen.main.bounds.height * 0.06)
        .overlay(RoundedRectangle(cornerRadius: 10)
            .stroke(Color.blue, lineWidth: 1.5))
            .padding(.bottom, 15)
      })

    }
    .navigationBarTitle("")
    .navigationBarHidden(true)
  }
}
第二种观点:

struct SignUpView: View {
  @Environment(\.presentationMode) var present
  var body: some View {
    NavigationView{
      Button(action: {
        self.present.wrappedValue.dismiss()
        }, label: {
        ZStack {
        Image(systemName: "chevron.backward")
          .font(.system(size: 25, weight: .semibold))
          .foregroundColor(.blue)
          .padding(.leading, 10)
        }
      })
    }
    .navigationBarTitle("")
    .navigationBarHidden(true)
  }
}

将此添加到您的
注册视图中

init() {
    UINavigationBar.appearance().isUserInteractionEnabled = false
    UINavigationBar.appearance().backgroundColor = .clear
    UINavigationBar.appearance().barTintColor = .clear
    UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
    UINavigationBar.appearance().shadowImage = UIImage()
    UINavigationBar.appearance().tintColor = .clear
}
并删除:

.navigationBarHidden(true)
这个想法:

.navigationBarHidden(true)
似乎不仅隐藏了条,而且还禁用了滑动。所以,为了能够滑行,我们不能这样做。第四,我们也可以使用
UINavigationBar.appearance()
全局隐藏它,而不需要
.navigationBarHidden(true)

请注意,如果在应用程序中的任何其他位置希望返回默认的
UINavigationBar
,则必须再次
init()
,并将其取消隐藏,因为这会全局设置
外观

我建议您使用一个全局可访问的函数,其中包含隐藏和取消隐藏代码(如果您需要的话)