-
Notifications
You must be signed in to change notification settings - Fork 22
/
BasicsViewController.swift
178 lines (152 loc) · 6.45 KB
/
BasicsViewController.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
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
//
// BasicsViewController.swift
// Rickenbacker
//
// Created by Condy on 2021/10/2.
//
import Foundation
import UIKit
open class BasicsViewController: UIViewController {
/// 强制移除,开启之后`viewDidDisappear`时,此页面会从堆栈中移除。
/// When you jump to the next view controller after opening, this view controller will be removed from the stack.
public var wasForceRemoved = false
public var navigationTitle = "" {
didSet {
self.navigationItem.title = navigationTitle
}
}
public var realSelf: UIViewController {
get {
var realSelf: UIViewController = self
while let par = realSelf.parent, !(par is UINavigationController) {
realSelf = par
}
return realSelf
}
}
public override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
self.hidesBottomBarWhenPushed = true
}
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.hidesBottomBarWhenPushed = true
}
deinit {
Log.debug("\(self.description): Deinited", file: "\(type(of: self))")
}
override open func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.hidesBackButton = true
self.view.backgroundColor = UIColor.white
self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true
if #available(iOS 11.0, *) {
UIScrollView.appearance().contentInsetAdjustmentBehavior = .never
} else {
self.automaticallyAdjustsScrollViewInsets = false
}
self.setPopOrDismissButton(width: 50)
}
// MARK: - public methods
open func setPopOrDismissButton(width: CGFloat) {
guard let navigationController = self.navigationController else {
return
}
if let _ = self.presentingViewController, navigationController.viewControllers.first == self {
self.navigationItem.leftBarButtonItem = createBarButtonItem(image: Res.base_black_close, width: width)
} else if navigationController.viewControllers.first != self {
self.navigationItem.leftBarButtonItem = createBarButtonItem(image: Res.base_black_back, width: width)
}
}
/// About to close the current page. Click the back button or gesture to return.
open func setWillCloseByUserBlock(_ block: @escaping (_ vc: BasicsViewController) -> Void) {
self.willCloseByUserBlock = block
}
/// It has been successfully closed. Click the back button or gesture to return.
open func setClosedByUserComplete(_ block: @escaping (_ vc: BasicsViewController) -> Void) {
self.closedByUserCompletionBlock = block
}
/// Customize the back button click event.
@objc dynamic open func backAction() {
popoutOrDismiss()
}
open func popoutOrDismiss(completion: (() -> Void)? = nil) {
self.isPopedFromBackButton = true
self.willCloseByUserBlock?(self)
if self.presentingViewController != nil {
if let count = self.navigationController?.viewControllers.count, count > 1, self.navigationController?.topViewController == realSelf {
popout(completion: completion)
} else if self.navigationController?.children[0] == self {
self.dismiss(animated: true) {
completion?()
self.closedByUserCompletionBlock?(self)
}
} else {
popout(completion: completion)
}
} else {
popout(completion: completion)
}
}
// MARK: - private methods
private var isPopedFromBackButton = false
private var hasNaviWhenWillDisappear = false
private var willCloseByUserBlock: ((_ vc: BasicsViewController) -> Void)?
private var closedByUserCompletionBlock: ((_ vc: BasicsViewController) -> Void)?
private func popout(completion: (() -> Void)? = nil) {
if let completion = completion {
CATransaction.begin()
CATransaction.setCompletionBlock(completion)
self.navigationController?.popViewController(animated: true)
self.closedByUserCompletionBlock?(self)
CATransaction.commit()
} else {
self.navigationController?.popViewController(animated: true)
self.closedByUserCompletionBlock?(self)
}
}
private func viewDidPopOut() {
if !self.isPopedFromBackButton {
self.willCloseByUserBlock?(self)
self.closedByUserCompletionBlock?(self)
}
}
private func createBarButtonItem(image: UIImage?, width: CGFloat) -> UIBarButtonItem {
let button = UIButton(type: .custom)
button.setImage(image, for: .normal)
button.addTarget(self, action: #selector(BasicsViewController.backAction), for: .touchUpInside)
button.sizeToFit()
button.frame = CGRect(x: 0, y: 0, width: width, height: 44)
button.contentHorizontalAlignment = .left
return UIBarButtonItem.init(customView: button)
}
}
extension BasicsViewController {
override open func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
setupNavigationBarHiddenableViewWillAppear(animated)
}
override open func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
setupNavigationBarHiddenableViewWillDisappear(animated)
self.hasNaviWhenWillDisappear = self.navigationController != nil
}
override open func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
setupNavigationBarHiddenableViewDidAppear(animated)
}
override open func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
guard wasForceRemoved, let viewControllers = navigationController?.viewControllers else {
return
}
if let controllers = self.navigationController?.viewControllers, !controllers.contains(realSelf) {
self.viewDidPopOut()
} else if self.navigationController == nil && hasNaviWhenWillDisappear {
self.viewDidPopOut()
}
navigationController?.viewControllers = viewControllers.compactMap({ vc in
return vc == self.realSelf ? nil : vc
})
}
}