美文网首页
SwiftUI: 自定义返回按钮

SwiftUI: 自定义返回按钮

作者: 芮淼一线 | 来源:发表于2021-07-22 18:52 被阅读0次
    一:NavigationView:实现自定义返回按钮

    使用NavigationView,NavigationLink实现导航效果:

    struct BackView: View {
        var array = ["1", "2", "33"]
        var body: some View {
            NavigationView {
                List(array, id: \.self) { value in
                    NavigationLink(destination: DetailView()) {
                        Text(value)
                    }
                    .isDetailLink(false)//隐藏自带的返回按钮
                }
               //.navigationBarTitle("Navigation")
               .navigationBarTitle("Navigation",displayMode: .inline)//设置NavView显示模式
            }
        }
    }
    

    自定义返回按钮:

    struct DetailView: View {
        @Environment(\.presentationMode) var presentationMode
    
        var body: some View {
            VStack {
    
            }
            .navigationBarItems(leading: Button(action: {
                print("back action...")
                self.presentationMode.wrappedValue.dismiss()
            }, label: {
                Text("Back")
                    .foregroundColor(.white)
            })
            )
        }
    }
    

    扩展实现侧滑返回:

    //扩展实现侧滑返回
    extension UINavigationController: UIGestureRecognizerDelegate {
        override open func viewDidLoad() {
            super.viewDidLoad()
            interactivePopGestureRecognizer?.delegate = self
        }
    
        public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
            return viewControllers.count > 1
        }
    }
    

    相关文章

      网友评论

          本文标题:SwiftUI: 自定义返回按钮

          本文链接:https://www.haomeiwen.com/subject/zhlymltx.html