SearchBar Overrides Section Title View

I put a searchBar in a tableHeaderView. Everything works fine on iphone 6, but on iphone 5s am I getting this weird result?

override func viewDidLoad() { super.viewDidLoad() tableView.delegate = self tableView.dataSource = self tableView.sectionIndexColor = Constants.Colors.ThemeGreen tableView.sectionIndexBackgroundColor = UIColor.clearColor() tableView.sectionIndexTrackingBackgroundColor = UIColor.clearColor() tableView.contentInset = UIEdgeInsetsMake(0, 0, CGFloat(Constants.Dimensions.TabBarHeight), 0) resultSearchController = UISearchController(searchResultsController: nil) resultSearchController.searchResultsUpdater = self resultSearchController.dimsBackgroundDuringPresentation = false resultSearchController.definesPresentationContext = true tableView.tableHeaderView = resultSearchController.searchBar resultSearchController.searchBar.sizeToFit() //Fetch data for the first time do{ try fetchedResultsController.performFetch() listHeaderView?.count = "\(fetchedResultsController.fetchedObjects!.count)" }catch{ print("Error - Couldn't fetch list") } 
  • NOTE. I am using NSFetchedResultController to retrieve data.

enter image description here

+7
ios iphone uitableview autolayout uisearchbar
source share
3 answers

Here is the solution. Do not call sizeToFit () AFTER putting the searchBar in the tableHeaderView, but call it BEFORE. What the hell is going on behind the scenes ... Interesting.

 resultSearchController.searchBar.sizeToFit() //Important to call sizeToFit BEFORE adding it to tableHeaderView or you get layout issues tableView.tableHeaderView = resultSearchController.searchBar 
+3
source share

It seems that only below the line of code works ... now nothing similar works.

 self.tableView.beginUpdates() // self.tableView.setTableHeaderView(headerView: self.filterView!) //self.tableView.reloadData() self.tableView.layoutIfNeeded() self.tableView.layoutSubviews() self.tableView.endUpdates() 

every time your table resizes or changes constraints, you must call the code above.

0
source share

Try it, maybe it will work

resultSearchController.clipToBounds = true

You can also try the same thing as

 searchControl.searchBar.clipToBounds = true 

I think this works for your code.

0
source share

All Articles