logo头像
Snippet 博客主题

个人Demo搭建01

主要用于快速搭建项目,平常用到的一些第三方和自己整理的一些UI控件,常用的功能模块等。

1. 创建桥接文件

https://www.jianshu.com/p/4da697a06ef0

2. 项目基础目录

image.png

3. base基类

BaseViewController

主要实现功能

  • 控制状态栏的颜色
  • 网络诊断
  • 返回按钮统样式和点击事件统一处理
  • 接收登录成功和退出等通知
  • 弹出窗信息统一处理
  • 空白图处理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//  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")
}
}
微信打赏

赞赏是不耍流氓的鼓励