【发布时间】:2021-12-30 21:41:52
【问题描述】:
我正在一个 ViewController 中实现一个集合视图控制器,它有一个变量是 UISearchController,我尝试根据这个源代码初始化它https://github.com/codeWithCal/TableViewExample/blob/SearchFilter/TableViewExample/TableViewController.swift
我唯一的问题是 searchBar 及其过滤器范围没有显示。这是我的代码
父类型:
class ContactListParent:UIViewController, UISearchResultsUpdating, UISearchBarDelegate
变量:
let search = UISearchController()
初始化函数:
func initSearchCont(){
search.loadViewIfNeeded()
search.searchResultsUpdater = self
search.obscuresBackgroundDuringPresentation = false
search.searchBar.enablesReturnKeyAutomatically = false
search.searchBar.returnKeyType = UIReturnKeyType.done
definesPresentationContext = true
navigationItem.searchController = search
navigationItem.hidesSearchBarWhenScrolling = false
search.searchBar.scopeButtonTitles = ["All", "Dogs", "Cats"]
search.searchBar.delegate = self
}
viewDidLoad 周期:
super.viewDidLoad()
initArr()
// INIT Below
initSearchCont()
//
// search.view.backgroundColor = .yellow
self.view.addSubview(search.view)
let layout = UICollectionViewFlowLayout()
contVC = ContactVC(collectionViewLayout: layout)
contVC?.CLV = self
self.addChild(contVC!) //100kb
contVC?.view.frame = CGRect(x: 0, y: bounds.height*0.2, width: bounds.width, height: bounds.height)
self.view.addSubview(contVC!.view)
contVC?.didMove(toParent: self)
【问题讨论】: