【问题标题】:UISearchController not showing in VCUISearchController 未在 VC 中显示
【发布时间】: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)

【问题讨论】:

    标签: swift uisearchcontroller


    【解决方案1】:

    在此处删除不需要的搜索变量,如果您已分配给 navigationitem,则无需添加 addsubview 即可查看。

    这是在 NavigationBar 中显示 SearchBar 的示例代码。

    注意:确保将 UINavigationController 分配给您的 视图控制器

    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
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多