-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathVMScrollViewController.swift
69 lines (59 loc) · 2.52 KB
/
VMScrollViewController.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
//
// VMScrollViewController.swift
// Rickenbacker
//
// Created by Condy on 2022/5/20.
//
import RxSwift
import ObjectiveC
/// 兼容其他类型的控制器,只要是使用`UIScrollView`或其子类的均可采用
open class VMScrollViewController<T: ViewModel>: VMViewController<T> {
public private(set) var scrollView: UIScrollView
public init(scrollView: UIScrollView, viewModel: T) {
self.scrollView = scrollView
super.init(viewModel: viewModel)
}
public convenience init(scrollView: UIScrollView) {
self.init(scrollView: scrollView, viewModel: T.init())
}
public required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
open override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.view.addSubview(self.scrollView)
self.scrollView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0),
scrollView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0),
scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
])
if self is NavigationBarHiddenable {
self.scrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
} else {
let statusBarHeight: CGFloat = {
if #available(iOS 13.0, *) {
let statusManager = UIApplication.shared.windows.first?.windowScene?.statusBarManager
return statusManager?.statusBarFrame.height ?? 20.0
} else {
return UIApplication.shared.statusBarFrame.height
}
}()
self.scrollView.contentInset = UIEdgeInsets(top: statusBarHeight+44, left: 0, bottom: 0, right: 0)
}
// 是否包含刷新模块
#if RICKENBACKER_MJREFRESH
self.setupOptionalRefresh()
#endif
// 是否包含空数据模块
#if RICKENBACKER_DZNEMPTYDATASET
self.setupOptionalEmptyData()
#endif
}
/// 需要添加dynamic修饰,否则无法使用方法交换
/// Need to add dynamic modification, otherwise the method exchange cannot be used
//dynamic func setupScrollSubRefresh() { }
//dynamic func setupScrollSubEmptyData() { }
}