ToolbarView.swift
3.75 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
//
// UINavigationToolbar.swift
// browser
//
// Created by Artem Talko on 27.09.2023.
//
import UIKit
enum ToolbarElementType {
case back
case forward
case share
case history
case tabs
}
final class ToolbarView: UIView {
var action: ((ToolbarElementType) -> Void)?
private let toolbar: UIToolbar = {
let obj = UIToolbar()
obj.sizeToFit()
obj.barTintColor = ColorConstants.customLightBlue
obj.clipsToBounds = true
return obj
}()
let backBarButtonItem: UIBarButtonItem = {
let obj = UIBarButtonItem()
obj.isEnabled = false
obj.image = UIImage(systemName: "chevron.backward")
return obj
}()
let forwardBarButtonItem: UIBarButtonItem = {
let obj = UIBarButtonItem()
obj.isEnabled = false
obj.image = UIImage(systemName: "chevron.forward")
return obj
}()
let shareBarButtonItem: UIBarButtonItem = {
let obj = UIBarButtonItem()
obj.isEnabled = false
obj.image = UIImage(systemName: "square.and.arrow.up")
return obj
}()
private let historyBarButtonItem: UIBarButtonItem = {
let obj = UIBarButtonItem()
obj.image = UIImage(systemName: "book")
return obj
}()
private let tabsBarButtonItem: UIBarButtonItem = {
let obj = UIBarButtonItem()
obj.image = UIImage(systemName: "square.on.square")
return obj
}()
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setup() {
addSubview(toolbar)
setupToolbar()
initActions()
setupConstraints()
}
private func setupConstraints() {
toolbar.snp.makeConstraints { make in
make.top.equalToSuperview()
make.leading.trailing.equalToSuperview()
make.bottom.equalTo(safeAreaLayoutGuide.snp.bottom)
}
}
}
// MARK: - Helpers
extension ToolbarView {
private func initActions() {
backBarButtonItem.action = #selector(didItemTapped(_:))
backBarButtonItem.target = self
forwardBarButtonItem.action = #selector(didItemTapped(_:))
forwardBarButtonItem.target = self
shareBarButtonItem.action = #selector(didItemTapped(_:))
shareBarButtonItem.target = self
historyBarButtonItem.action = #selector(didItemTapped(_:))
historyBarButtonItem.target = self
tabsBarButtonItem.action = #selector(didItemTapped(_:))
tabsBarButtonItem.target = self
}
private func setupToolbar() {
let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
toolbar.items = [
backBarButtonItem,
flexibleSpace,
forwardBarButtonItem,
flexibleSpace,
shareBarButtonItem,
flexibleSpace,
historyBarButtonItem,
flexibleSpace,
tabsBarButtonItem,
]
}
}
// MARK: - Actions
extension ToolbarView {
@objc
private func didItemTapped(_ sender: UIBarButtonItem) {
guard let action else {
return
}
switch sender {
case backBarButtonItem:
action(.back)
case forwardBarButtonItem:
action(.forward)
case shareBarButtonItem:
action(.share)
case historyBarButtonItem:
action(.history)
case tabsBarButtonItem:
action(.tabs)
default:
break
}
}
}