美文网首页
TabView 隐藏

TabView 隐藏

作者: EnjoyWT | 来源:发表于2023-02-16 21:03 被阅读0次

    增加新类

    import SwiftUI
    import Combine
    struct SUIView: UIViewRepresentable {
    
        func updateUIView(_ uiView: UIView, context: Context) {
            
        }
        
    
    
        func makeCoordinator() -> Coordinator {
        }
        func makeUIView(context: Context) -> UIView {
            //设置mapView的代理
           let mapView = UIView()
            mapView.backgroundColor = .green
          
            
            return mapView
        }
        
    }
    
    
    class TabBarHandle {
        var tabBar: UITabBar?
        var tabController:UITabBarController?
        var tabBarViewFrame: CGRect?
        
        func hidenTabView(){
            self.tabBarViewFrame = tabController?.view.frame
            let height = tabBar!.frame.height
    
            self.tabController?.tabBar.hide()
            tabController?.view.frame = CGRect(x:0, y:0, width:tabBarViewFrame!.width, height:tabBarViewFrame!.height + height);
    
        }
        func showTabView(){
            self.tabController?.tabBar.show()
            guard let tabBarFrame = tabBarViewFrame else {
                return
            }
    
            self.tabController?.view.frame = tabBarFrame
        }
    }
    struct TabBarExtractor: UIViewControllerRepresentable {
        @Binding var tabBarHandel: TabBarHandle?
    
        func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {}
    
        func makeUIViewController(context: Context) -> some UIViewController {
            let controller = ViewController()
           
            controller.onTabBarAppearanceHandle = { (tb, tbc) in
                tabBarHandel?.tabBar = tb
                tabBarHandel?.tabController = tbc
    
            }
            return controller
        }
     
    }
    
    private extension TabBarExtractor {
        class ViewController: UIViewController {
            var onTabBarAppearanceHandle: ((UITabBar, UITabBarController) -> Void)?
    
            override func viewWillAppear(_ animated: Bool) {
                super.viewWillAppear(animated)
    
                if let tabBar = self.tabBarController?.tabBar {
                    onTabBarAppearanceHandle?(tabBar, self.tabBarController!)
                } else {
                    print("Could not locate TabBar! Try change extractor place in views hierarchy.")
                }
            }
        }
    }
    import UIKit
    
    extension UITabBar {
        func toggleVisibility() {
            if isHidden {
                show()
            } else {
                hide()
            }
        }
    
        func show() {
            guard isHidden else { return }
    
            let visibleY = frame.origin.y
            let hiddenY = visibleY + frame.height
            frame.origin.y = hiddenY
            isHidden = false
    
            UIView.animate(withDuration: 0.25) { [weak self] in
                self?.frame.origin.y = visibleY
            }
        }
    
        func hide() {
            guard !isHidden else { return }
    
    //        let visibleY = frame.origin.y
    //        let hiddenY = visibleY + frame.height
            self.isHidden = true
            
            /* 动画会造成view布局问题*/
    //        UIView.animate(withDuration: 0.25) { [weak self] in
    //            self?.frame.origin.y = hiddenY
    //        } completion: { [weak self] completed in
    //            guard completed else { return }
    //            self?.isHidden = true
    //
    //            self?.frame.origin.y = visibleY
    //        }
        }
    }
    
    

    调用方法

    struct SecondView: View {
        @State private var tabbarhandle:TabBarHandle? = TabBarHandle()
        var body: some View {
            VStack {
                NavigationView
                {
                    NavigationLink(destination: ThirdView().onAppear {
                        tabbarhandle?.hidenTabView()
                    }.onDisappear(perform: {
                        tabbarhandle?.showTabView()
    
                    })) {
                        Text("Second View, tap to navigate")
                            .font(.headline)
                    }
    
                }.background{
                    TabBarExtractor(tabBarHandel: $tabbarhandel)
                }
          
            }
            .navigationTitle("Second title")
            .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .center)
            .background(Color.orange)
        }
    }
    

    参考文章1
    参考文章2

    相关文章

      网友评论

          本文标题:TabView 隐藏

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