BrowserHomeView.swift
5.64 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//
// MainView.swift
// browser
//
// Created by Artem Talko on 25.09.2023.
//
import UIKit
import SnapKit
final class BrowserHomeView: GradientView {
let removeAdvertSegmentControl: UISegmentedControl = {
let items = StringConstants.segmentedControlItems
let obj = UISegmentedControl(items: items)
obj.selectedSegmentIndex = 1
obj.backgroundColor = ColorConstants.segmentControlBackground
obj.layer.cornerRadius = 10
obj.selectedSegmentTintColor = UIColor(red: 0.349, green: 0.565, blue: 1, alpha: 1)
obj.setTitleTextAttributes([.foregroundColor: UIColor.black], for: .normal)
obj.setTitleTextAttributes([.foregroundColor: UIColor.white], for: .selected)
return obj
}()
let settingButton: UIButton = {
let obj = UIButton()
obj.setImage(UIImage(named: "Settings")?.withTintColor(.systemBlue), for: .normal)
return obj
}()
private let titleLabel: UILabel = {
let obj = UILabel()
obj.text = StringConstants.frequentlyVisited
obj.font = FontConstants.semiboldFont_18
return obj
}()
let frequentlyVisitedCollectionView: UICollectionView = {
let obj = UICollectionViewFlowLayout()
obj.minimumInteritemSpacing = 12.sizeW
obj.scrollDirection = .horizontal
obj.sectionInset = UIEdgeInsets(top: 4.sizeH, left: 4.sizeW, bottom: 4.sizeH, right: 4.sizeW)
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: obj)
collectionView.backgroundColor = .clear
collectionView.showsHorizontalScrollIndicator = false
return collectionView
}()
let toolbarView: ToolbarView = {
let obj = ToolbarView()
obj.backgroundColor = ColorConstants.customLightBlue
return obj
}()
let searchBarContainer: SearchBarContainer = {
let obj = SearchBarContainer()
obj.backgroundColor = .clear
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(titleLabel)
addSubview(frequentlyVisitedCollectionView)
addSubview(removeAdvertSegmentControl)
addSubview(settingButton)
addSubview(searchBarContainer)
addSubview(toolbarView)
setupConstraints()
}
private func setupConstraints() {
removeAdvertSegmentControl.snp.makeConstraints { make in
make.top.equalTo(safeAreaLayoutGuide.snp.top).offset(16.sizeH)
make.leading.equalToSuperview().offset(54.sizeW)
make.trailing.equalTo(settingButton.snp.leading).offset(-15.sizeW)
make.height.equalTo(34.sizeH)
}
settingButton.snp.makeConstraints { make in
make.trailing.equalToSuperview().offset(-16.sizeW)
make.height.equalTo(24.sizeH)
make.width.equalTo(24.sizeH)
make.centerY.equalTo(removeAdvertSegmentControl)
}
titleLabel.snp.makeConstraints { make in
make.top.equalTo(removeAdvertSegmentControl.snp.bottom).offset(64.sizeH)
make.leading.equalTo(safeAreaLayoutGuide.snp.leading).offset(16.sizeW)
}
frequentlyVisitedCollectionView.snp.makeConstraints { make in
make.top.equalTo(titleLabel.snp.bottom).offset(8.sizeH)
make.leading.trailing.equalToSuperview().inset(24.sizeW)
make.height.equalTo(120.sizeH)
}
toolbarView.snp.makeConstraints { make in
make.leading.equalToSuperview()
make.bottom.equalToSuperview()
make.trailing.equalToSuperview()
}
searchBarContainer.snp.makeConstraints { make in
make.leading.equalToSuperview()
make.bottom.equalToSuperview().offset(-90.sizeH)
make.trailing.equalToSuperview()
}
}
}
//MARK: - childView extension
extension BrowserHomeView {
func addSearchView(_ childView: UIView) {
addSubview(childView)
childView.snp.makeConstraints { make in
make.top.equalToSuperview()
make.leading.trailing.equalToSuperview()
make.bottom.equalTo(searchBarContainer.snp.top).offset(-2.sizeH)
}
}
}
//MARK: - Search bar extention
extension BrowserHomeView {
func animateSearchBar(_ keyboardHeight: CGFloat) {
UIView.animate(withDuration: 0.3) {
self.searchBarContainer.backgroundColor = ColorConstants.customLightBlue
self.searchBarContainer.searchBarView.backgroundColor = .black
self.searchBarContainer.searchBarView.searchTextFieldView.textColor = .white
self.searchBarContainer.snp.updateConstraints { make in
make.leading.equalToSuperview()
make.trailing.equalToSuperview()
make.bottom.equalToSuperview().offset(-keyboardHeight)
}
self.layoutIfNeeded()
}
}
func animateSearchBarDismiss() {
BrowserHomeView.animate(withDuration: 0.3) {
self.searchBarContainer.searchBarView.backgroundColor = ColorConstants.customLightBlue
self.searchBarContainer.snp.updateConstraints { make in
make.leading.equalToSuperview()
make.trailing.equalToSuperview()
make.bottom.equalToSuperview().offset(-90.sizeH)
}
}
self.layoutIfNeeded()
}
}