-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathViewController07_UpdateNavigationBar.swift
122 lines (96 loc) · 4.11 KB
/
ViewController07_UpdateNavigationBar.swift
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
//
// ViewController07_UpdateNavigationBar.swift
// NXNavigationExtensionDemo
//
// Created by lidan on 2021/11/9.
//
import UIKit
class ViewController07_UpdateNavigationBar: BaseViewController {
private static let randomColorButtonWidthAndHeight = CGFloat(160.0)
private lazy var randomColorButton: UIButton = {
let randomColorButton = UIButton(type: .custom)
randomColorButton.backgroundColor = .clear
randomColorButton.translatesAutoresizingMaskIntoConstraints = false
randomColorButton.layer.cornerRadius = ViewController07_UpdateNavigationBar.randomColorButtonWidthAndHeight * 0.5
randomColorButton.layer.borderWidth = 5.0
randomColorButton.setTitle("Update", for: .normal)
randomColorButton.addTarget(self, action: #selector(clickRandomColorButton(_:)), for: .touchUpInside)
return randomColorButton
}()
private lazy var backButtonTitle = "Custom"
private lazy var randomDark = UIColor.randomDark
private lazy var randomLight = UIColor.randomLight
private var currentRandom: UIColor {
return UIColor.customColor { [weak self] in
return self?.randomLight ?? UIColor.randomLight
} darkModeColor: { [weak self] in
return self?.randomDark ?? UIColor.randomDark
}
}
private var isDarkMode: Bool {
return randomColorButton.tag % 2 == 0
}
@objc
private func clickRandomColorButton(_ button: UIButton) {
button.tag += 1
backButtonTitle = "😜(\(button.tag))"
randomDark = UIColor.randomDark
randomLight = UIColor.randomLight
updateRandomColorButtonState()
nx_setNeedsNavigationBarAppearanceUpdate()
setNeedsStatusBarAppearanceUpdate()
}
private func updateRandomColorButtonState() {
randomColorButton.layer.borderColor = currentRandom.cgColor
randomColorButton.setTitleColor(currentRandom, for: .normal)
if #available(iOS 13.0, *) {
randomColorButton.setTitleColor(currentRandom.resolvedColor(with: view.traitCollection), for: .normal)
}
}
convenience init() {
self.init(nibName: nil, bundle: nil)
navigationItem.title = NSLocalizedString(NavigationFeatureItem.Style.updateNavigationBar.rawValue, comment: "")
}
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(randomColorButton)
updateRandomColorButtonState()
NSLayoutConstraint.activate([
randomColorButton.widthAnchor.constraint(equalToConstant: ViewController07_UpdateNavigationBar.randomColorButtonWidthAndHeight),
randomColorButton.heightAnchor.constraint(equalToConstant: ViewController07_UpdateNavigationBar.randomColorButtonWidthAndHeight),
randomColorButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
randomColorButton.centerYAnchor.constraint(equalTo: view.centerYAnchor),
])
}
override var preferredStatusBarStyle: UIStatusBarStyle {
if #available(iOS 13.0, *) {
return isDarkMode ? .lightContent : .darkContent
} else {
return isDarkMode ? .lightContent : .default
}
}
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
updateRandomColorButtonState()
}
override var randomColor: UIColor {
return currentRandom
}
}
extension ViewController07_UpdateNavigationBar {
override var nx_navigationBarBackgroundColor: UIColor? {
return currentRandom
}
override var nx_barTintColor: UIColor? {
return isDarkMode ? .white : .black
}
override var nx_titleTextAttributes: [NSAttributedString.Key : Any]? {
return [NSAttributedString.Key.foregroundColor: nx_barTintColor ?? (isDarkMode ? .white : .black)]
}
override var nx_useSystemBackButton: Bool {
return true
}
override var nx_systemBackButtonTitle: String? {
return backButtonTitle
}
}