I want to place a search bar above a uicollection view like the image below. I want to do this programmatically.
current view
This is my code for search bar setup function. I have it in the home view controller.
func setupSearchBar() {
let searchBar = UISearchBar(frame: CGRect(x: 0, y: 64, width:UIScreen.main.bounds.width, height: 32))
searchBar.barTintColor = UIColor(red: 64/255, green: 64/255, blue: 64/255, alpha: 1)
searchBar.backgroundColor = UIColor.blue
searchBar.isTranslucent = true
searchBar.placeholder = "Search Timeline"
searchBar.searchBarStyle = UISearchBarStyle.prominent
view.addSubview(searchBar)
}
For the listing details of each item, people use UITableView because it shows more info on each item. The UICollectionView class manages an ordered collection of data items and presents them using customizable layouts.
Select the Collection Reusable View of the header, set the identifier as “HeaderView” in the Attributes inspector. For the Collection Reusable View of the footer, set the identifier as “FooterView”.
There are no section headers in the UICollectionView. So for your first task, you'll add a new section header using the search text as the section title. To display this section header, you'll use UICollectionReusableView .
From apple's documentation, UICollectionView is: An object that manages an ordered collection of data items and presents them using customizable layouts. The name and definition makes it clear, it is a way to display a Collection of UI Views in our app. The individual view is referred as a Cell.
You can add the search bar to your UICollectionview header.
This creates the searchBar programmatically
lazy var searchBar : UISearchBar = {
let s = UISearchBar()
s.placeholder = "Search Timeline"
s.delegate = self
s.tintColor = .white
s.barTintColor = // color you like
s.barStyle = .default
s.sizeToFit()
return s
}()
Next, in your view did load register the header view.
collectionView?.register(UICollectionViewCell.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "headerCellId")
Override the below method to define the height you would like your header to be.
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: view.frame.width, height: 40)
}
Last, add search bar to UICollectionview header, defining the constraints to fit the entire header view.
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
}
Try to add these code:
// Call sizeToFit() on the search bar so it fits nicely in the UIView
self.searchController!.searchBar.sizeToFit()
// For some reason, the search bar will extend outside the view to the left after calling sizeToFit. This next line corrects this.
self.searchController!.searchBar.frame.size.width = self.collectionView!.frame.size.width
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With