// // ADHBaseVC.swift // ADHTuanCan // // Created by 敖德亨 on 2023/9/30. // import UIKit import RxSwift import RxCocoa import IQKeyboardManagerSwift //import MJRefresh class ADHBaseVC: UIViewController { /// 导航栏 var navBar: MCNavBarDelegate?{ didSet{ self.addNavBar() } } /// 内容距离顶部的高度约束 var navBarHeightConstraint: NSLayoutConstraint?{ didSet{ if let navBarHeightConstraint = navBarHeightConstraint { self.navBar?.navBarHeightSubject.bind(to: navBarHeightConstraint.rx.constBind).disposed(by: disposeAutoRelease) } } } /// 设置空数据scrollView var emptyDataScrollView : UIScrollView?{ didSet{ self.setEmptyDataScrollView() } } /// hud 提示 // lazy var hud : MCHud! = { // return MCHud() // }() lazy var hud : MCHud! = { return MCHud() }() /// 自动释放 lazy var disposeAutoRelease = { return DisposeBag() }() @objc dynamic func navBack(){ self.navigationController?.popViewController(animated: true) } //MARK: - override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = kTBackgroundColor IQKeyboardManager.shared.shouldResignOnTouchOutside = true self.navigationController?.navigationBar.isHidden = true self.view.backgroundColor = kTBackgroundColor self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) // self.sx_disableInteractivePop = false } deinit { NotificationCenter.default.removeObserver(self) self.emptyDataScrollView?.emptyDataSetSource = nil self.emptyDataScrollView?.emptyDataSetDelegate = nil self.emptyDataScrollView = nil //处理定时器的释放 Mirror(reflecting: self).children.forEach { (child) in if let _ = child.label{ if child.value is DispatchSourceTimer{ (child.value as? DispatchSourceTimer)?.cancel() } } } DLog("\(self) dealloc") } } //MARK: - Private Method extension ADHBaseVC{ /// 添加navBar private func addNavBar(){ for view in self.view.subviews{ if view is MCNavBarDelegate{ view.removeFromSuperview() break } } if let navBar = navBar { self.view.addSubview(navBar) navBar.navBarBackBlock = {[unowned self] in self.navBack() } navBar.mas_makeConstraints { make in make?.left.top()?.right()?.mas_equalTo()(self.view) make?.height.offset()(navBar.navBarHeight()) } _ = navBar.navBarHeightSubject.takeUntil(self.rx.deallocated).subscribe(onNext:{_ in navBar.mas_updateConstraints { make in make?.height.offset()(navBar.navBarHeight()) } }) } } } //MARK: - 创建一个空数据或者错误页面 extension ADHBaseVC{ /// 直接显示错误界面 /// - Parameter status: <#status description#> /// - Returns: <#description#> @discardableResult final func showEmptyView(_ status : MCHttpStatus = .noData) -> UIView{ let emptyView = UINib.view(withType: DZNEmptyLoadView.self)! var text : String? var image : UIImage? if status == .noData{ text = self.emptyTitle() } switch status{ case .noData: text = self.emptyTitle() image = UIImage.init(named: self.emptyImage()) case .noNetwork: text = self.noNetworkTitle() image = UIImage.init(named: self.noNetworkImage()) case .error: text = self.errorTitle() image = UIImage.init(named: self.errorImage()) case .timeout: text = self.networkTimeOutTitle() image = UIImage.init(named: self.failureImage()) default:break } emptyView.emptyImageView.image = image emptyView.emptyLabel.text = text self.view.addSubview(emptyView) self.view.bringSubviewToFront(emptyView) emptyView.mas_makeConstraints { (make) in make?.right.left()?.bottom()?.mas_equalTo()(self.view) make?.top.mas_equalTo()(self.navBar == nil ? self.view:self.navBar!.mas_bottom) } emptyView.activity.isHidden = !(status == .normal) emptyView.emptyLabel.isHidden = (status == .normal) emptyView.emptyImageView.isHidden = (status == .normal) return emptyView } }