Ios UISearchController搜索栏动画第一次非常慢

Ios UISearchController搜索栏动画第一次非常慢,ios,swift,uisearchbar,uisearchcontroller,Ios,Swift,Uisearchbar,Uisearchcontroller,在iOS 9中,我使用UISearchController并在UIViewController中显示其搜索栏,当我第一次点击搜索栏时,我经历了很多延迟,并且尝试了我所能想到的一切,但都没有结果……下面是我的代码以及延迟发生的视频链接——延迟发生在模拟器和我的设备上 func setupUI() { self.view.backgroundColor = UIColor.whiteColor() // Required to properly display searchbar

在iOS 9中,我使用UISearchController并在UIViewController中显示其搜索栏,当我第一次点击搜索栏时,我经历了很多延迟,并且尝试了我所能想到的一切,但都没有结果……下面是我的代码以及延迟发生的视频链接——延迟发生在模拟器和我的设备上

func setupUI() {
    self.view.backgroundColor = UIColor.whiteColor()

    // Required to properly display searchbar within nav & tabbar controllers
    self.extendedLayoutIncludesOpaqueBars = true // have tried setting this to false as well
    self.definesPresentationContext = true

    self.searchResultsController = AppDelegate.getViewController(ScheduleStoryboard.name, controllerName: ScheduleStoryboard.Identifiers.foodSearchResults) as? SearchResultsController

    self.searchController = UISearchController(searchResultsController: searchResultsController)
    self.searchController.searchResultsUpdater = self
    self.searchController.delegate = self
    self.searchController.dimsBackgroundDuringPresentation = true

    self.searchController.searchBar.delegate = self
    self.searchController.searchBar.placeholder = "Search foods..."
    self.searchController.searchBar.setBackgroundImage(UIImage(named: "background-searchbar")?.resizableImageWithCapInsets(UIEdgeInsetsMake(0, 0, 0, 0)), forBarPosition: .Any, barMetrics: .Default)
    self.searchController.searchBar.tintColor = UIColor.whiteColor()
    self.searchController.searchBar.sizeToFit()

    // this headerView does NOT belong to the tableView, its anchored on top of the tableView so that the searchbar remains fixed when scrolling
    self.headerView.addSubview(searchController.searchBar)

    self.tableView.delegate = self
    self.tableView.dataSource = self
    self.tableView.tableHeaderView?.backgroundColor = UIColor.clearColor()
    self.tableView.tableHeaderView?.addBorder(.Bottom, color: UIColor.groupTableViewBackgroundColor(), width: 0.25)

    self.segmentedControl.tintColor = UIColor.genioBlue()
}
下面是一个视频链接,显示发生了什么:


谢谢

我只创建过一次搜索控制器,但我使用UITableViewController作为基类。以下是我的实现:

class SearchController: UITableViewController {
    let searchController = UISearchController(searchResultsController: nil)
    var items:[ArrayOfYourType]
    var filteredItems:[ArrayOfYourType]
    var scopeTitles:[String]?

override func viewDidLoad() {
    super.viewDidLoad()

    searchController.searchResultsUpdater = self
    searchController.dimsBackgroundDuringPresentation = false
    definesPresentationContext = true
    tableView.tableHeaderView = searchController.searchBar

    searchController.searchBar.scopeButtonTitles = scopeTitles
    searchController.searchBar.delegate = self
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if searchController.active {
        return filteredItems.count
    }
    return items.count
}

func filterContentForSearchText(searchText: String, scope: String = "All") {
    filteredItems = items.filter { item in
        //return true or false depending on your filter
        return true
    }
    dispatch_async(dispatch_get_main_queue(), { () -> Void in
        self.tableView.reloadData()
    })
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle,
        reuseIdentifier: nil)
    let item: String
    let category: String
    if searchController.active {
        item = filteredItems[indexPath.row].getTitle()
        category = filteredItems[indexPath.row].getCategory()
    }
    else {
        item = items[indexPath.row].getTitle()
        category = items[indexPath.row].getCategory()
    }
    cell.textLabel?.text = item
    cell.detailTextLabel?.text = category
    return cell
}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    //your code here
}
}

//MARK: UISearchResultsUpdating
extension SearchController: UISearchResultsUpdating {
    func updateSearchResultsForSearchController(searchController: UISearchController) {
        if let _ = scopeTitles {
            let searchBar = searchController.searchBar
            let scope = searchBar.scopeButtonTitles![searchBar.selectedScopeButtonIndex]
            filterContentForSearchText(searchController.searchBar.text!,scope:scope)
        }
        else {
            filterContentForSearchText(searchController.searchBar.text!)
        }
    }
}

//MARK: UISearchBarDelegate
extension SearchController: UISearchBarDelegate {
    func searchBar(searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
        filterContentForSearchText(searchBar.text!, scope: searchBar.scopeButtonTitles![selectedScope])
    }
}

我希望这有帮助:)

您用于背景搜索栏的图像是否可能太大


创建渐变可能会更快。这里有一个很好的

您是否找到了解决方法?我面临着完全相同的问题,我也面临着同样的问题。我也没有找到解决方案。你在模拟器上测试吗?还是在你的iPhone或iPad上?模拟器通常是有缺陷的,所以可以连接一个实际的设备进行测试。希望有帮助!如果有,请告诉我。还是一样的问题吗?我也有同样的问题。第一次之后,一切都很顺利。我不知道这个错误的真正原因是什么,但就我而言,这是因为我在
searchResultsController
tableView中添加了
UIRefreshControl
。删除它,不再播放慢速动画。如果链接中断,请在答案中包含解决方案,否则答案将丢失。