CNLiveBaseViewController.swift
8.72 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//
// 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
}
rightNavSpace = CGRectGetMaxX(lastBtn!.frame)
viewWillLayoutSubviews()
}
}
private var leftNavSpace: CGFloat = 0.0
private var rightNavSpace: CGFloat = 0.0
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 + 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 > 1 {
if viewControllers?.last == 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)
}
}