| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- //
- // BaseViewController.swift
- // MTP2_iOS
- //
- // Created by 方振兴 on 2018/3/23.
- // Copyright © 2018年 Muchinfo. All rights reserved.
- //
- import UIKit
- import NVActivityIndicatorView
- import JXSegmentedView
- /// 管理类容器基类
- class BaseViewController: UIViewController {
-
- // MARK: - 属性列表
- /// 无数据按钮
- @IBOutlet weak var noDataButton: UIButton! {
- didSet {
- noDataButton.set(image: #imageLiteral(resourceName: "noData-universal"), title: "noData".localized, titlePosition: .bottom, additionalSpacing: 3.0, state: .normal)
- noDataButton.tintColor = UIColor.lightGray
- noDataButton.isHidden = true
- }
- }
-
- /// 兼容IOS 11
- var insert: UIEdgeInsets {
- var insets = UIEdgeInsets.zero
- if #available(iOS 11.0, *) {
- insets = UIApplication.shared.delegate?.window??.safeAreaInsets ??
- UIEdgeInsets.zero
- }
- return insets
- }
- /// 动画对象
- var _anim: NVActivityIndicatorView?
- /// appDelegate
- let appDelegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
-
- /// 当前所处位置
- var selectedIndex: Int = 0
- /// 数据携带
- var takeInfo: Any?
- /// indicator
- let indicator: JXSegmentedIndicatorImageView = {
- $0.indicatorWidth = 40.0
- $0.indicatorHeight = 10.0
- $0.image = UIImage(named: "segment_line")
- return $0
- } (JXSegmentedIndicatorImageView())
-
- // MARK: - 生命周期
- required init?(coder aDecoder: NSCoder) {
- super.init(coder: aDecoder)
-
- /// 设置Tabbar Item
- self.tabBarItem.setTitleTextAttributes( [NSAttributedString.Key.foregroundColor: Color_BarItem_Selected], for: .selected)
- self.tabBarItem.setTitleTextAttributes( [NSAttributedString.Key.foregroundColor: Color_BarItem_Normal], for: .normal)
- }
-
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do any additional setup after loading the view.
- addBackBarButtonItem(false)
-
- UINavigationBar.appearance().barTintColor = UIColorFromHex(rgbValue: 0x2481DD)
- UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
- }
-
- deinit {
- dPrint("[\(#function)] \(self.description) 被释放了")
- }
-
- override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
- self.view.endEditing(true)
- }
-
- // MARK: - Loding动画
- /// 加载页面
- func addLoadingView() {
- /// Loading
- _anim = NVActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 50, height: 50), type: .ballClipRotate, color: UIColorFromHex(rgbValue: 0xA8B6CC), padding: 0)
- _anim!.center = self.view.center
- self.view.addSubview(_anim!)
- }
-
- /// 添加返回按钮
- /// - Parameter isHidden: 是否显示
- func addBackBarButtonItem(_ isHidden: Bool = false) {
- if !isHidden {
- self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "back"), style: .done, target: self, action: #selector(onBackBarButtonItemPressed))
- self.navigationItem.leftBarButtonItem?.tintColor = .white
- } else {
- self.navigationItem.leftBarButtonItem = nil
- }
- }
-
- // MARK: - 页面跳转
- /// present
- /// - Parameters:
- /// - sbname: sbname
- /// - sbId: sbId
- /// - isNav: isNav
- /// - style: modalTransitionStyle
- /// - takeInfo: takeInfo
- /// - checkLogin: checkLogin
- func present(storyboardName sbname:String,
- storyboardId sbId:String,
- needNavigationController isNav: Bool = true,
- modalTransitionStyle style: UIModalTransitionStyle = .crossDissolve,
- takeInfo: Any? = nil,
- checkLogin: Bool = true) {
- if !checkLogin || MTP2BusinessCore.shared.getLoginStatus() { /// 检查登录状态
- let board: UIStoryboard = UIStoryboard.init(name: sbname, bundle: nil)
- let viewController = board.instantiateViewController(withIdentifier: sbId) as? BaseViewController
- viewController?.takeInfo = takeInfo
-
- if isNav == true {
- let navigationController = BaseNavigationController(rootViewController: viewController!)
- navigationController.modalPresentationStyle = .overFullScreen
- navigationController.modalTransitionStyle = style
- self.present(navigationController, animated: true, completion: nil)
- } else {
- viewController?.modalPresentationStyle = .overFullScreen
- viewController?.modalTransitionStyle = style
- self.present(viewController!, animated: true, completion: nil)
- }
- } else { /// 未登录
- let login = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Login")
- let navigationController = BaseNavigationController(rootViewController: login)
- navigationController.modalPresentationStyle = .overFullScreen
- navigationController.modalTransitionStyle = style
- self.present(navigationController, animated: true, completion: nil)
- }
- }
-
- /// 页面消失
- @IBAction func dismiss() {
- /// 返回上层视图
- self.dismiss(animated: true, completion: {})
- }
-
- /// 返回按钮点击事件
- @IBAction func onBackBarButtonItemPressed() {
- if (self.navigationController?.viewControllers.count ?? 0) > 1 {
- self.navigationController?.popViewController(animated: true)
- } else {
- self.dismiss(animated: true, completion: {})
- }
- }
-
- // MARK: - 系统权限相关
- /// 系统权限设置
- func openSystemSetting() {
- /// 进入系统权限设置
- /// 这里去引导用户去获取相关权限
- if #available(iOS 10.0, *) {
- UIApplication.shared.open(URL.init(string: UIApplication.openSettingsURLString)!, options: [:], completionHandler: { (isCanOpen) in
-
- })
- } else {
- // Fallback on earlier versions
- UIApplication.shared.openURL(URL.init(string: UIApplication.openSettingsURLString)!)
- }
- }
- }
- extension BaseViewController: UIGestureRecognizerDelegate {
-
- func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
- return true
- }
-
- func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
- return !(self.children.count == 1)
- }
- }
|