【问题标题】:SwiftUI - Custom NavigationBar back button disables sliding gestureSwiftUI - 自定义导航栏后退按钮禁用滑动手势
【发布时间】:2020-09-27 00:52:37
【问题描述】:

我在 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)
  }
}

【问题讨论】:

    标签: swiftui


    【解决方案1】:

    将此添加到您的SignUpView

    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() 并取消隐藏它,因为这会全局设置appearance

    如果您是这种情况,我建议您使用一个包含隐藏和取消隐藏代码的全局可访问函数。

    【讨论】:

      猜你喜欢
      • 2019-10-27
      • 1970-01-01
      • 2021-09-08
      • 2016-04-01
      • 2014-01-16
      • 2016-11-24
      • 2023-03-27
      • 2012-01-03
      • 2020-01-07
      相关资源
      最近更新 更多