[英]UISearchController not showing in VC
I am implementing a collection view controller in a ViewController which has a variable that is a UISearchController, i tried initializing it thanks based on this source code https://github.com/codeWithCal/TableViewExample/blob/SearchFilter/TableViewExample/TableViewController.swift
我唯一的問題是 searchBar 及其過濾器 scope 沒有顯示。 這是我的代碼
父類型:
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)
刪除此處不需要的搜索變量,如果您分配給navigationitem,則無需添加addsubview 即可查看。
這是它將在 NavigationBar 中顯示 SearchBar 的示例代碼。
注意:確保將 UINavigationController 分配給 ViewController
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
configureSearchbar()
}
func configureSearchbar(){
let searchController = UISearchController(searchResultsController: nil)
searchController.searchBar.placeholder = "Search"
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
navigationItem.searchController = searchController
self.title = "Contacts" //Navigation Title
self.definesPresentationContext = true
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.