//
//  WA_RootNAV.swift
//  wallpaper_project
//
//  Created by 忆海16 on 2024/3/11.
//

import UIKit

class WA_RootNAV: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()

        setBackItmer()
    }
    

    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
            if self.viewControllers.count <= 1 {
                let viewController = UIViewController()
                viewController.hidesBottomBarWhenPushed = true
                tabBarController?.tabBar.isHidden = true
                return false
            }
            setBackItmer()
            return true
        }
    
    
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        if viewControllers.count > 0 {
//            setBackItmer()/
            viewController.hidesBottomBarWhenPushed = true
            tabBarController?.tabBar.isHidden = true
        }
        setBackItmer()
        super.pushViewController(viewController, animated: animated)

    }
    func setBackItmer(){
        // 返回按钮
        let backButton = UIButton(type: .custom)

        // 给按钮设置返回箭头图片
        backButton.setBackgroundImage(#imageLiteral(resourceName: "ic_arrow_left"), for: .normal)

        // 设置frame
//        backButton.frame = CGRect(x: 200, y: 13, width: 18, height: 18)
        backButton.addTarget(self, action: #selector(back), for: .touchUpInside)

//        // 自定义导航栏的UIBarButtonItem类型的按钮
        let backView = UIBarButtonItem(customView: backButton)
//
//        // 重要方法，用来调整自定义返回view距离左边的距离
        let barButtonItem = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
        barButtonItem.width = -5

        // 返回按钮设置成功
        navigationItem.leftBarButtonItems = [barButtonItem, backView]
        
    }
        //
        @objc func back(){
            navigationController?.popViewController(animated: true)
            
        }

}
