主要用于快速搭建项目,平常用到的一些第三方和自己整理的一些UI控件,常用的功能模块等。
1. 创建桥接文件
https://www.jianshu.com/p/4da697a06ef0
2. 项目基础目录
![](https://img.haomeiwen.com/i1747706/0ba27b72d19b7540.png)
3. base基类
BaseViewController
主要实现功能
- 控制状态栏的颜色
- 网络诊断
- 返回按钮统样式和点击事件统一处理
- 接收登录成功和退出等通知
- 弹出窗信息统一处理
- 空白图处理
// BaseViewController.swift
import UIKit
import Reachability
@objc protocol BaseViewControllerDelegate {
@objc optional func backController(controller:BaseViewController)
}
class BaseViewController: UIViewController,BaseViewControllerDelegate,DataHanderProtocol {
//状态条(电池栏)颜色:计算属性
override var preferredStatusBarStyle: UIStatusBarStyle{
return .default//白色
}
let reachability = try! Reachability()
weak var backDelegate:BaseViewControllerDelegate?
lazy var backButton:UIButton = {
let button = UIButton()
// black_back
button.setImage(UIImage.init(named: "black_back"), for: UIControl.State.normal)
button.setImage(UIImage.init(named: "black_back"), for: UIControl.State.highlighted)
button.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
button.addTarget(self, action: #selector(backBtnClick(button:)), for: UIControl.Event.touchUpInside)
button.contentEdgeInsets = UIEdgeInsets.init(top: 0, left: -20, bottom: 0, right: 0)
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
DLog("控制器\(String(describing: self))-------被加载")
// self.navigationController?.navigationBar.isTranslucent = false
view.backgroundColor = AppColor.vcBgColor
//接受登录通知
LFNotice.add(observer: self, selector: #selector(loginSuccess), notification: LFNotice.Notification.login)
//接受退出通知
LFNotice.add(observer: self, selector: #selector(loginOut), notification: LFNotice.Notification.loginOut)
//接受是否重新登录通知
LFNotice.add(observer: self, selector: #selector(needLogin), notification: LFNotice.Notification.needLogin)
}
//刷新数据
@objc func refreshData(){
}
@objc private func needLogin(){
// if self.navigationController?.isVisible == true {
// let vc = PXSLoginVC()
// self.navigationController?.pushViewController(vc)
//
// }
}
// 警告框,提示没有连接网络 *********************
func alert_noNetwrok() {
self.view.ly_emptyView = PXSEmptyView.diyNoNetworkEmptyWithTarget(target: self, action: #selector(refreshData))
self.view.ly_showEmpty()
}
// 有网*********************
func alert_Netwrok(){
self.view.ly_hideEmpty()
}
deinit {
DLog("控制器\(String(describing: self))-------被释放")
reachability.stopNotifier()
NotificationCenter.default.removeObserver(self, name: .reachabilityChanged, object: reachability)
LFNotice.removeAll()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if self.isRootViewController() == false {
self.backDelegate = self
self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(customView: backButton)
}
NotificationCenter.default.addObserver(self, selector: #selector(reachabilityChanged(note:)), name: .reachabilityChanged, object: reachability)
do{
try reachability.startNotifier()
}catch{
print("could not start reachability notifier")
}
}
}
//MARK: - 接收通知
extension BaseViewController {
@objc func loginSuccess(){
}
@objc func loginOut(){
defaultUserInfo.token = nil
LFSaveTool.remove(file: "LoginModel")
// LFCache.removeObjectForKey(userModelKey)
// LFCache.removeAllObjects()
}
@objc func reachabilityChanged(note: Notification) {
let reachability = note.object as! Reachability
switch reachability.connection {
case .wifi:
DLog("Reachable via WiFi")
DispatchQueue.main.async {
self.alert_Netwrok()
}
case .cellular:
DLog("Reachable via Cellular")
DispatchQueue.main.async {
self.alert_Netwrok()
}
case .unavailable:
DLog("Network not reachable")
DispatchQueue.main.async { // 不加这句导致界面还没初始化完成就打开警告框,这样不行
self.alert_noNetwrok() // 警告框,提示没有网络
}
case .none:
DLog("")
}
}
}
//MARK: - 返回按钮
extension BaseViewController {
func setWhiteNavBar(){
self.navBarBackgroundAlpha = 0
self.navBarBarTintColor = AppColor.white
self.navBarTintColor = AppColor.black
self.navBarTitleColor = AppColor.black
}
func setBlackNavBar(){
self.navBarBackgroundAlpha = 0
self.navBarBarTintColor = AppColor.white
self.navBarTintColor = AppColor.white
self.navBarTitleColor = AppColor.white
}
func isRootViewController()-> Bool {
return self == self.navigationController?.viewControllers.first
}
@objc private func backBtnClick(button:UIButton){
if self.backDelegate != nil {
self.backDelegate?.backController?(controller: self)
}
}
func backController(controller: BaseViewController) {
self.navigationController?.popViewController(animated: true)
}
}
//MARK: - Toast
extension BaseViewController {
func lf_show(message:String) {
self.view.makeToast(message, duration: 2.0, position: "CSToastPositionCenter")
}
func lf_showInWindown(message:String) {
kkwindow?.makeToast(message, duration: 2.0, position: "CSToastPositionCenter")
}
}
网友评论