You can add a search bar to your UICollectionview header.
This creates a programmatic searchBar search.
lazy var searchBar : UISearchBar = {
let s = UISearchBar()
s.placeholder = "Search Timeline"
s.delegate = self
s.tintColor = .white
s.barTintColor =
s.barStyle = .default
s.sizeToFit()
return s
}()
Next, the header register is loaded in your view.
collectionView?.register(UICollectionViewCell.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "headerCellId")
Override the method below to determine the height at which you want your title to be.
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: view.frame.width, height: 40)
}
, UICollectionview, .
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "headerCellId", for: indexPath)
header.addSubview(searchBar)
searchBar.translatesAutoresizingMaskIntoConstraints = false
searchBar.leftAnchor.constraint(equalTo: header.leftAnchor).isActive = true
searchBar.rightAnchor.constraint(equalTo: header.rightAnchor).isActive = true
searchBar.topAnchor.constraint(equalTo: header.topAnchor).isActive = true
searchBar.bottomAnchor.constraint(equalTo: header.bottomAnchor).isActive = true
return header
}