This repository was archived by the owner on Sep 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 380
Expand file tree
/
Copy pathSearchSegmentBarCell.swift
More file actions
126 lines (99 loc) · 3.88 KB
/
SearchSegmentBarCell.swift
File metadata and controls
126 lines (99 loc) · 3.88 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
//
// SearchSegementBarCell.swift
// Freetime
//
// Created by Ehud Adler on 3/12/19.
// Copyright © 2019 Ryan Nystrom. All rights reserved.
//
import UIKit
import SnapKit
final class SearchSegmentBarCell: UICollectionViewCell, SearchableCell, UISearchBarDelegate {
weak var delegate: SearchBarCellDelegate?
private let searchBar = UISearchBar(frame: .zero)
private let segmentControl = UISegmentedControl(frame: .zero)
var segmentControlLeadingConstraint: Constraint!
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .white
searchBar.returnKeyType = .search
searchBar.enablesReturnKeyAutomatically = false
searchBar.searchBarStyle = .minimal
searchBar.delegate = self
contentView.addSubview(searchBar)
contentView.addSubview(segmentControl)
searchBar.snp.makeConstraints { make in
make.leading.centerY.equalTo(contentView)
make.trailing.equalTo(segmentControl.snp.leading)
}
segmentControl.snp.makeConstraints { make in
segmentControlLeadingConstraint = make.leading.equalTo(contentView.snp.trailing).constraint
make.trailing.equalTo(contentView.safeAreaLayoutGuide).inset(15)
make.top.bottom.equalTo(contentView)
}
segmentControl.addTarget(self, action: #selector(updateSegement), for: .valueChanged)
setSegmentControl()
segmentControlLeadingConstraint.deactivate()
searchBar.resignWhenKeyboardHides()
segmentControl.removeBorders()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
layoutContentView()
}
// MARK: Public API
func set(items: [String], selectedIndex: Int = 0) {
for title in items {
segmentControl.insertSegment(
withTitle: title,
at: segmentControl.numberOfSegments,
animated: true
)
}
segmentControl.selectedSegmentIndex = selectedIndex
}
func configure(query: String, placeholder: String) {
searchBar.text = query
searchBar.placeholder = placeholder
}
// MARK: Private API
func setSegmentControl() {
segmentControl.backgroundColor = .clear
segmentControl.tintColor = .clear
let normalFont: [AnyHashable: Any] = [
NSAttributedStringKey.foregroundColor: Styles.Colors.Gray.dark.color,
NSAttributedStringKey.font: UIFont.systemFont(ofSize: 15)
]
let selectedFont: [AnyHashable: Any] = [
NSAttributedStringKey.foregroundColor: Styles.Colors.Blue.medium.color,
NSAttributedStringKey.font: UIFont.systemFont(ofSize: 15)
]
segmentControl.setTitleTextAttributes(normalFont, for: .normal)
segmentControl.setTitleTextAttributes(selectedFont, for: .selected)
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
delegate?.didChangeSearchText(cell: self, query: searchText)
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchBar.resignFirstResponder()
}
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
UIView.animate(withDuration: 0.3) {
self.segmentControlLeadingConstraint.deactivate()
self.segmentControl.alpha = 1
self.layoutIfNeeded()
}
}
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
UIView.animate(withDuration: 0.3) {
self.segmentControlLeadingConstraint.activate()
self.segmentControl.alpha = 0
self.layoutIfNeeded()
}
}
@objc func updateSegement() {
delegate?.didChangeSegment(cell: self, index: self.segmentControl.selectedSegmentIndex)
}
}