其他分享
首页 > 其他分享> > swift 项目多个UITabBarController 进行push

swift 项目多个UITabBarController 进行push

作者:互联网

 

1.创建一个 普通的viewController ,添加需要push的 UITabBarController

/// 添加TabController的控制器
class JYJokerThroughRootController: JYBaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        /// TabController
        let jokerThroughRootVC = JYJokerThroughTabController()
        self.addChild(jokerThroughRootVC)
        self.view.addSubview(jokerThroughRootVC.view)
        ///隐藏导航栏:不隐藏会出现两个导航栏,一个是push之前的tab的导航栏, 一个是push之后的tab的导航栏
        JYNavDelegateManager.shareManager.addHiddNavBarControllerType(of: JYJokerThroughRootController.self)
    }
}

 

2. 如果新的tabBarController界面需要左上角显示返回按钮 , 可以自定义,pop的时候需要 用注意tabBarController?.navigationController进行pop, 不是 self.navigationController 进行pop

    private func testBack(){
        let backBtn = UIButton(type: UIButton.ButtonType.custom)
        backBtn.backgroundColor = UIColor.green
        backBtn.frame = CGRect(x: 0, y: 100, width: 100, height: 100)
        backBtn.setImage(UIImage.init(named: "back_normal_gray"), for: UIControl.State.normal)
        backBtn.addTarget(self, action: #selector(clcikBackBtn), for: UIControl.Event.touchUpInside)
        view.addSubview(backBtn)
    }




    @objc private func clcikBackBtn() {
        self.tabBarController?.navigationController?.popViewController(animated: true)
    }

 

 

3. 新的tabbarController 1级界面禁用手势, 不然和之前的导航栏手势冲突, 新的tab  push 多次之后,侧滑返回不是在新tab的 一次次返回,直接 返回到第一个tab上

    /// 是否允许收手势返回
    private var isCanSideBack: Bool = true


    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.forbidhenSideBack()
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.resetSideBack()
    }


// MARK: 一级界面禁止手势
extension JYJokerThroughHomeController: UIGestureRecognizerDelegate {
    
    /// 禁止使用手势返回
    func forbidhenSideBack() {
        self.isCanSideBack = false
        if (self.navigationController?.responds(to:#selector(getter: self.navigationController?.interactivePopGestureRecognizer))) == true {
            self.navigationController?.interactivePopGestureRecognizer?.delegate = self
        }
    }
    
    /// 恢复手势返回
    func resetSideBack() {
        self.isCanSideBack = true
        if (self.navigationController?.responds(to:#selector(getter: self.navigationController?.interactivePopGestureRecognizer))) == true {
            self.navigationController?.interactivePopGestureRecognizer?.delegate = nil
        }
    }
    
    /// 是否禁用手势
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        return self.isCanSideBack
    }
}

 

标签:true,self,UITabBarController,backBtn,func,push,animated,swift,navigationControll
来源: https://www.cnblogs.com/qingzZ/p/13214741.html