CNLiveBaseViewController.swift 9.13 KB
//
//  CNLiveBaseViewController.swift
//  metaCode
//
//  Created by open on 2023/5/30.
//

import UIKit
import Foundation

class CNLiveBaseViewController: UIViewController  {
    
    static let kTopNavLeftRightOffset = 6.0
    /// topNavView的高度 默认高度为 get_system_nav_height()
    public var _topNavViewHeight: CGFloat = 0.0
    public var topNavViewHeight : CGFloat {
        get {
            return _topNavViewHeight
        }
        set {
            _topNavViewHeight = newValue
            if (view.subviews.contains(topNavView)) {
                viewWillLayoutSubviews()
            }
        }
    }
    
    /// 是否隐藏导航栏(默认 隐藏)
    public var _navigationBarHidden: Bool = true
    public var navigationBarHidden: Bool {
        get {
            return _navigationBarHidden
        }
        set {
            _navigationBarHidden = newValue
            topNavView.isHidden = _navigationBarHidden
        }
    }
    
    /// 设置标题显示
    public var _titleName: String = ""
    public var titleName: String {
        get {
            return _titleName
        }
        set {
            _titleName = newValue
            super.title = _titleName
            titleLabel.text = _titleName
        }
    }
    
    public var _leftBtnWhite: Bool = false
    public var leftBtnWhite: Bool {
        get {
            return _leftBtnWhite
        }
        set {
            _leftBtnWhite = newValue
            //先设置一个默认返回按钮
            let leftBtn_array : Array<UIButton> = [UIButton.backItem(imageName: (_leftBtnWhite ? "mine_header_icon_white_back":"main_navi_left_icon_back"), target: self, action: #selector(ClickBackBtn))]
            leftNavBtns = leftBtn_array
        }
    }
    
    /// //顶部导航内容(不带topLine)偏移量Y 初始值为0
    public var _topNavViewSubUIContentOffsetY: CGFloat = 0.0
    public var topNavViewSubUIContentOffsetY: CGFloat {
        get {
            return _topNavViewSubUIContentOffsetY
        }
        set {
            _topNavViewSubUIContentOffsetY = newValue
            viewWillLayoutSubviews()
        }
    }
    
    public var _leftNavBtns: Array<UIButton> = []
    public var leftNavBtns: Array<UIButton> {
        get {
            return _leftNavBtns
        }
        set {
            _leftNavBtns = newValue
            var lastBtn : UIButton? = nil
            _leftNavBtns.forEach { btn in
                btn.removeFromSuperview()
                topNavView.addSubview(btn)
                btn.frame = CGRect(x: lastBtn != nil ? CGRectGetMaxX(lastBtn!.frame) : CNLiveBaseViewController.kTopNavLeftRightOffset, y: topNavViewHeight - btn.height, width: btn.width, height: btn.height)
                lastBtn = btn
            }
            leftNavSpace = CGRectGetMaxX(lastBtn!.frame)
            viewWillLayoutSubviews()
        }
    }
    
    public var _rightNavBtns: Array<UIButton> = []
    public var rightNavBtns: Array<UIButton> {
        get {
            return _rightNavBtns
        }
        set {
            _rightNavBtns = newValue
            var lastBtn : UIButton? = nil
            _rightNavBtns.forEach { btn in
                btn.removeFromSuperview()
                topNavView.addSubview(btn)
                btn.frame = CGRect(x: lastBtn != nil ? (lastBtn!.origin.x - btn.width) : (topNavView.width - CNLiveBaseViewController.kTopNavLeftRightOffset - btn.width), y: topNavViewHeight - btn.height, width: btn.width, height: btn.height)
                lastBtn = btn
            }
            if _rightNavBtns.count > 0{
                rightNavSpace = CGRectGetMaxX(lastBtn!.frame)
            }
            viewWillLayoutSubviews()
        }
    }
    
    private var leftNavSpace: CGFloat = 0.0
    private var rightNavSpace: CGFloat = 0.0
    
    /**
     是否禁止右滑返回
     */
    var popGestureRecognizerEnable:Bool = false
    
    deinit {
        log("主界面已销毁")
    }
    
    override func viewDidLoad() {
        self.view.backgroundColor = HexColor("#F7F8FA")
        
        if #available(iOS 13.0, *) {
            self.overrideUserInterfaceStyle = .light
        } else {
            // Fallback on earlier versions
        }
        
        configIOS11()
        configIOS15()
        
        initBaseUI()
        
        self.navigationController?.isNavigationBarHidden = true
    }
    
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        topNavView.frame = CGRect(x: 0, y: 0, width: view.width, height:topNavViewHeight)// topNavViewHeight +  get_system_statusBar_height())
        let lrSpace = leftNavSpace > rightNavSpace ? leftNavSpace : rightNavSpace
        titleLabel.frame = CGRect(x: CGFloat(Int(lrSpace)) , y: topNavViewHeight - get_system_nav_ui_height() - topNavViewSubUIContentOffsetY, width: topNavView.width - (lrSpace * 2), height: get_system_nav_ui_height())
        leftNavBtns.forEach { currentBtn in
            currentBtn.frame = CGRectMake(currentBtn.origin.x, topNavViewHeight - currentBtn.height - topNavViewSubUIContentOffsetY, currentBtn.width, currentBtn.height);
        }
        rightNavBtns.forEach { currentBtn in
            currentBtn.frame = CGRectMake(currentBtn.origin.x, topNavViewHeight - currentBtn.height - topNavViewSubUIContentOffsetY, currentBtn.width, currentBtn.height);
        }
        topLine.frame = CGRect(x: 0, y: topNavViewHeight - 1, width: topNavView.width, height: 1)
    }
    
    public lazy var topNavView: UIView = {
        let topNavView = UIView.init(frame: CGRect(x: 0, y: 0, width: view.width, height: topNavViewHeight))
        topNavView.backgroundColor = HexColor("#fafbfd")
        topNavView.isUserInteractionEnabled = true
        
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(topNavViewTap))
        tap.cancelsTouchesInView = false
        topNavView.isHidden = true
        topNavView.addGestureRecognizer(tap)
        topNavView.backgroundColor = HexColor("#F7F8FA")
        return topNavView
    }()
    
    public lazy var titleLabel: UILabel = {
        let titleLabel = UILabel.init(frame: CGRectMake(0, topNavView.height - get_system_nav_ui_height(), topNavView.frame.size.width, get_system_nav_ui_height()))
        titleLabel.textColor = HexColor("#333333")
        titleLabel.font = Font_18_Fit
        titleLabel.textAlignment = .center
        titleLabel.text = title
        return titleLabel
    }()
    
    public lazy var topLine: UIImageView = {
        let topLine = UIImageView.init(frame: CGRect(x: 0, y: topNavView.height - 1, width: view.width, height: 1))
        topLine.backgroundColor = .clear
        return topLine
    }()
    
    // MARK: - iOS 适配
    func configIOS11() {
        /// 适配 iOS 11.0 ,iOS11以后,控制器的automaticallyAdjustsScrollViewInsets已经废弃,所以默认就会是YES
        /// iOS 11新增:adjustContentInset 和 contentInsetAdjustmentBehavior 来处理滚动区域
        if #available(iOS 11.0, *) {
            UITableView.appearance().estimatedRowHeight = 0
            UITableView.appearance().estimatedSectionHeaderHeight = 0
            UITableView.appearance().estimatedSectionFooterHeight = 0
            // 防止列表/页面偏移
            UIScrollView.appearance().contentInsetAdjustmentBehavior = .never
            UITableView.appearance().contentInsetAdjustmentBehavior = .never
            UICollectionView.appearance().contentInsetAdjustmentBehavior = .never
        } else {
            self.automaticallyAdjustsScrollViewInsets = false
        }
    }
    
    func configIOS15() {
        // 适配iOS15,tableView的section设置
        // iOS15中,tableView会给每一个section的顶部(header以上)再加上一个22像素的高度,形成一个section和section之间的间距
        // 新增的sectionHeaderTopPadding会使表头新增一段间隙,默认为UITableViewAutomaticDimension
        if #available(iOS 15.0, *) {
            UITableView.appearance().sectionHeaderTopPadding = 0
        }
    }
    
    func initBaseUI() -> Void {
        self.topNavViewHeight = get_system_nav_height()
        
        view.addSubview(topNavView)
        topNavView.addSubview(titleLabel)
        topNavView.addSubview(topLine)
        
        leftBtnWhite = false
    }
    
    
    @objc func ClickBackBtn() {
        let viewControllers = self.navigationController?.viewControllers
        if viewControllers?.count ?? 0 > 1 {
            if viewControllers?.last == self {
                self.navigationController!.popViewController(animated: true)
            }else{
                viewControllers?.last?.children.forEach({ vc in
                    if vc == self{
                        self.navigationController!.popViewController(animated: true)
                    }
                })
            }
        }else{
            if (self.presentingViewController != nil) {
                self.dismiss(animated: true)
            }
        }
//        if (self.presentingViewController != nil) {
//            self.dismiss(animated: true)
//        }else{
//            self.navigationController!.popViewController(animated: true)
//        }
    }
    
    @objc func topNavViewTap() {
        UIApplication.shared.keyWindow?.endEditing(true)
    }
}