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 pathSearchBarCell.swift
More file actions
70 lines (52 loc) · 1.77 KB
/
SearchBarCell.swift
File metadata and controls
70 lines (52 loc) · 1.77 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
//
// SearchBarCell.swift
// Freetime
//
// Created by Ryan Nystrom on 5/14/17.
// Copyright © 2017 Ryan Nystrom. All rights reserved.
//
import UIKit
import SnapKit
protocol SearchableCell: class {}
protocol SearchBarCellDelegate: class {
func didChangeSearchText(cell: SearchableCell, query: String)
func didChangeSegment(cell: SearchableCell, index: Int)
}
final class SearchBarCell: UICollectionViewCell, SearchableCell, UISearchBarDelegate {
weak var delegate: SearchBarCellDelegate?
private let searchBar = UISearchBar(frame: .zero)
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)
searchBar.snp.makeConstraints { make in
make.leading.equalTo(contentView)
make.trailing.equalTo(contentView)
make.centerY.equalTo(contentView)
}
searchBar.resignWhenKeyboardHides()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
layoutContentView()
}
// MARK: Public API
func configure(query: String, placeholder: String) {
searchBar.text = query
searchBar.placeholder = placeholder
}
// MARK: Private API
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
delegate?.didChangeSearchText(cell: self, query: searchText)
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchBar.resignFirstResponder()
}
}