searchBar与节标题视图重叠

Mik*_*ael 7 iphone uitableview uisearchbar ios autolayout

我把searchBar放在tableHeaderView中.在iphone 6上一切正常,但在iphone 5s上,我得到了这个奇怪的结果?

 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")
    }
Run Code Online (Sandbox Code Playgroud)
  • 注意:我正在使用NSFetchedResultController来检索数据

在此输入图像描述

Mik*_*ael 3

这是解决方案。不要在将 searchBar 放入 tableHeaderView 之后调用 sizeToFit() ,而应在之前调用它。幕后到底发生了什么……我想知道……

resultSearchController.searchBar.sizeToFit() //Important to call sizeToFit BEFORE adding it to tableHeaderView or you get layout issues
tableView.tableHeaderView = resultSearchController.searchBar
Run Code Online (Sandbox Code Playgroud)