Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

searchBar overlapped by section header view

I put the searchBar inside the tableHeaderView. Everything works fine on iphone 6 but on iphone 5s I get 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'm using a NSFetchedResultController to retrieve the data

enter image description here

like image 969
Mikael Avatar asked Feb 09 '16 05:02

Mikael


Video Answer


1 Answers

Here is the solution. Don't call sizeToFit() AFTER putting the searchBar in tableHeaderView but call it BEFORE. What the hell is going on behind the scene... I wonder..

resultSearchController.searchBar.sizeToFit() //Important to call sizeToFit BEFORE adding it to tableHeaderView or you get layout issues
tableView.tableHeaderView = resultSearchController.searchBar
like image 70
Mikael Avatar answered Oct 21 '22 04:10

Mikael