searchBar overlapped by section header view

杀马特。学长 韩版系。学妹 提交于 2019-12-08 17:19:21

问题


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


回答1:


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



回答2:


It seems only below line of code does work ... nothing else seems to work right now.

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

everytime your table resize or change constraints you need to call above code.




回答3:


Try this it may work

resultSearchController.clipToBounds = true

You can also try the same as

searchControl.searchBar.clipToBounds = true

I think it works for your code.



来源:https://stackoverflow.com/questions/35284605/searchbar-overlapped-by-section-header-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!