宽窄优行-由【嘉易行】项目成品而来
younger_times
2023-07-05 0d8f5fc8a516bfd07e425909e4a4432600572ee7
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
//
//  YYViewController.swift
//  YYBase
//
//  Created by alvin_y on 2020/3/6.
//  Copyright © 2020 yangwang. All rights reserved.
//
 
import UIKit
import RxCocoa
import RxSwift
import MBProgressHUD
import DZNEmptyDataSet
 
class YYViewController: UIViewController,UIGestureRecognizerDelegate {
    // 调用此属性会自动拦截pop
    open var yy_popBlock:(() -> Void)?
    
    /// 导航栏黑线
    var nav_bar_hair_line_imageView : UIImageView?
    
    let disposeBag = DisposeBag()
    
    /// MBProgressHUD
    var hud : MBProgressHUD?
    
    // 导航栏返回按钮图片
    open var yy_nav_back_img:UIImage = UIImage.init(named: "icon_back") ?? UIImage.init() {
        didSet {
            if yy_isHiddenBackItem {
                yy_isHiddenBackItem = true
            } else {
                let btn = navigationItem.leftBarButtonItem?.customView as! UIButton
                btn.setImage(yy_nav_back_img, for: .normal)
            }
        }
    }
    
    // 是否需要返回按钮
    open var yy_isHiddenBackItem:Bool = false {
        didSet {
            if yy_isHiddenBackItem {
                navigationItem.leftBarButtonItem = UIBarButtonItem.init(title: "", style: .done, target: nil, action: nil)
            } else {
                navigationItem.leftBarButtonItem = UIBarButtonItem.yy_creat(image: yy_nav_back_img, target: self, alignment:.left, action: #selector(backItemEvent)).item
            }
        }
    }
    
    // 是否需要返回手势
    open var yy_isEnabledBackTap:Bool = true {
        didSet {
            if yy_isEnabledBackTap {
                self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true
            } else {
                self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
            }
        }
    }
    
    /// 是否需要导航栏黑线
    open var yy_isHiddenNavBarLine:Bool = true {
        didSet {
            if yy_isHiddenNavBarLine {
                nav_bar_hair_line_imageView?.isHidden = true
            } else {
                nav_bar_hair_line_imageView?.isHidden = false
            }
        }
    }
 
    override func viewDidLoad() {
        super.viewDidLoad()
 
        // Do any additional setup after loading the view.
        yy_isHiddenBackItem = false
        // 隐藏之后navigationBar不存在导致cash
        if let nav = navigationController?.navigationBar{
           nav_bar_hair_line_imageView = findHairlineImageViewUnder(view: nav)
        }
        view.backgroundColor = UIColor(hexString: "#F3F4F5")
 
 
        self.navigationController?.navigationBar.isTranslucent = false
 
 
        let attributes = [NSAttributedString.Key.foregroundColor:UIColor.color(light: UIColor.color(hexString: "#000000"), dark: UIColor.color(hexString: "#FFFFFF")),NSAttributedString.Key.font: UIFont.init(name: Medium, size: 18) ?? UIFont.systemFont(ofSize: 18)]
        let bartinColor = UIColor.color(light: UIColor.color(hexString: "#FFFFFF"), dark: UIColor.color(hexString: "#191919"))
 
        //修复导航栏问题
        if #available(iOS 15.0, *) {
            let bar = UINavigationBarAppearance()
            bar.configureWithOpaqueBackground() //消除15的黑框
            bar.backgroundEffect = nil
            bar.shadowColor = nil
            bar.titleTextAttributes = attributes
            bar.backgroundColor = UIColor.white.withAlphaComponent(1)
            navigationController?.navigationBar.scrollEdgeAppearance = bar //顶部透明
            navigationController?.navigationBar.standardAppearance = bar
        }else {
            navigationController?.navigationBar.titleTextAttributes  = attributes
            navigationController?.navigationBar.barTintColor = bartinColor
        }
        navigationController?.navigationBar.isTranslucent = false
 
        setupViews()
        defineLayouts()
        bindRx()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.interactivePopGestureRecognizer?.delegate = self
        
    }
    
    //MARK: - 初始化或者配置所有的view,此方法将在UIViewController的viewDidLoad()方法中调用
    func setupViews()  {
        
    }
    
    //MARK: - 定义布局,在这里添加约束,此方法将在UIViewController的viewDidLoad()方法中调用
    func defineLayouts()  {
        
    }
    
    //MARK: - 绑定ViewModel中的数据,此方法将在UIViewController的viewDidLoad()方法中调用
    func bindRx() {
        
    }
    
    //MARK: - NavigationController
    // pop点击事件
    @objc fileprivate func backItemEvent() {
        
        // 拦截pop事件
        if (yy_popBlock != nil) {
            yy_popBlock?()
            return
        }
        
        yy_pop()
    }
    
    /// UIStatusBarStyle
    override var preferredStatusBarStyle: UIStatusBarStyle{
        return .default
    }
    
    /// 找到导航栏的线条并选择显示或隐藏
    /// - Parameter view: 导航栏View
    func findHairlineImageViewUnder(view: UIView) -> UIImageView?{
        if view.isKind(of: UIImageView.classForCoder()) && view.bounds.size.height <= 1.0{
            return view as? UIImageView
        }
        for subView in view.subviews {
            let imageView = self.findHairlineImageViewUnder(view: subView)
            if (imageView != nil){
                return imageView
            }
        }
        return nil
    }
    
    
    /// pop
    /// - Parameter animated: animated
    open func yy_pop(_ animated:Bool = true) {
        navigationController?.popViewController(animated: animated)
    }
    
    /// yy_popToRoot
    /// - Parameter animated: animated
    open func yy_popToRoot(_ animated: Bool = true) {
        navigationController?.popToRootViewController(animated: animated)
    }
    
    open func yy_popToVC(index:Int) {
        guard let vc = navigationController?.viewControllers[index] else {
            return
        }
        navigationController?.popToViewController(vc, animated: true)
    }
    
    /// yy_push
    /// - Parameters:
    ///   - vc: UIViewController
    ///   - animated: animated
    ///   - hiddenBottom: hiddenBottom
    open func yy_push(vc:UIViewController, _ animated:Bool = true, _ hiddenBottom:Bool = true) {
        vc.hidesBottomBarWhenPushed = hiddenBottom
        self.navigationController?.pushViewController(vc, animated: animated)
    }
    
    /// push
    ///
    /// - Parameters:
    ///   - vc: UIViewController
    ///   - animated: animated
    ///   - hiddenBottom: hiddenBottom
    open func wy_pushAnimate(vc:UIViewController, _ animated:Bool = false, _ hiddenBottom:Bool = true){
        let transition = CATransition()
        transition.duration = 0.3
        transition.type = .moveIn
        transition.subtype = .fromTop
        navigationController?.view.layer.add(transition, forKey: kCATransition)
        vc.hidesBottomBarWhenPushed = hiddenBottom
        navigationController?.pushViewController(vc, animated: animated)
    }
    
    
    /// yy_present
    /// - Parameter vc: UIViewController
    open func yy_present(vc:UIViewController){
        self.present(vc, animated: true, completion: nil)
    }
    
    
    /// yy_present
    /// - Parameter vc: UIViewController
    open func yy_presentFullScreen(vc:UIViewController){
        vc.modalPresentationStyle = .custom
        vc.transitioningDelegate = self
        vc.view.frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT)
        self.present(vc, animated: true, completion: nil)
    }
    
    //MARK: - MBProgressHUD
    /// 不带文字
    func show()  {
        hud = build()
    }
    
    func hide()  {
        hud?.hide(animated: true)
    }
    
    private func build() -> MBProgressHUD {
        let hud = MBProgressHUD.showAdded(to: UIApplication.shared.keyWindow!, animated: true)
        hud.mode = .indeterminate
        hud.bezelView.style = .solidColor
        hud.bezelView.color = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.7)
        hud.customView?.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
        return hud
    }
}
 
 
// MARK: - UIViewControllerTransitioningDelegate
extension YYViewController:UIViewControllerTransitioningDelegate{
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return DismissAnimation()
    }
    
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return PresentAnimation()
    }
}
 
extension UIViewController {
    
    /// 当前屏幕上的UIViewController
    /// - Parameter base: Window
    class func base(_ viewController: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
        if let nav = viewController as? UINavigationController {
            return base(nav.visibleViewController)
        }
        if let tab = viewController as? UITabBarController {
            return base(tab.selectedViewController)
        }
        if let presented = viewController?.presentedViewController {
            return base(presented)
        }
        return viewController
    }
}
// 把颜色转成图片
func imageFromColor(color: UIColor, viewSize: CGSize) -> UIImage{
    
    let rect: CGRect = CGRect(x: 0, y: 0, width: viewSize.width, height: viewSize.height)
    
    UIGraphicsBeginImageContext(rect.size)
    
    let context: CGContext = UIGraphicsGetCurrentContext()!
    
    context.setFillColor(color.cgColor)
    
    context.fill(rect)
    let image = UIGraphicsGetImageFromCurrentImageContext()
    
    UIGraphicsGetCurrentContext()
    
    return image!
    
}
 
 
extension YYViewController{
    func aKeyLogin(){
        
    }
}