簡體   English   中英

searchBar與節標題視圖重疊

[英]searchBar overlapped by section header view

我把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")
    }
  • 注意:我正在使用NSFetchedResultController來檢索數據

在此輸入圖像描述

這是解決方案。 將searchBar放入tableHeaderView之后不要調用sizeToFit(),而是在BEFORE之前調用它。 現場到底發生了什么......我想知道..

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

似乎只有下面的代碼行才有效......現在似乎沒有其他工作可行。

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

每當您的表調整大小或更改您需要調用上面代碼的約束時。

試試這個可行

resultSearchController.clipToBounds = true

您也可以嘗試相同的

searchControl.searchBar.clipToBounds = true

我認為它適用於您的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM