IOS swift如何使子视图显示在搜索栏下方

IOS swift如何使子视图显示在搜索栏下方,ios,swift,uitableview,uisearchbar,addsubview,Ios,Swift,Uitableview,Uisearchbar,Addsubview,我在表单中嵌入了一个UISearchBar,所以我要做的是,一旦有人单击搜索栏,我希望一个TableView作为子视图弹出。我有正确的工作,但我希望子视图出现在搜索栏下,以便用户可以继续搜索,我的子视图接管了整个页面。下面是我如何尝试让功能正常工作的:Trulia应用程序。 请注意现在带有相应视图的搜索栏如果单击搜索栏,搜索栏正下方会显示一个子视图。如前所述,对于我来说,除了子视图占据整个页面,并且不显示在搜索栏下面之外,其他一切都正常工作。这是我的代码,下面的第三张图片是我的故事板,这是我的代

我在表单中嵌入了一个UISearchBar,所以我要做的是,一旦有人单击搜索栏,我希望一个TableView作为子视图弹出。我有正确的工作,但我希望子视图出现在搜索栏下,以便用户可以继续搜索,我的子视图接管了整个页面。下面是我如何尝试让功能正常工作的:Trulia应用程序。 请注意现在带有相应视图的搜索栏如果单击搜索栏,搜索栏正下方会显示一个子视图。如前所述,对于我来说,除了子视图占据整个页面,并且不显示在搜索栏下面之外,其他一切都正常工作。这是我的代码,下面的第三张图片是我的故事板,这是我的代码

       class RegistrationViewController: UIViewController,UISearchBarDelegate {

    @IBOutlet weak var Location: UISearchBar!
       func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
        searchBar.setShowsCancelButton(false, animated: true)

        let Popup = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "zip_searches") as! ExampleTableViewController
        self.addChildViewController(Popup)
        Popup.view.frame = self.view.frame
        self.view.addSubview(Popup.view)
        Popup.didMove(toParentViewController: self)
        return true
    }
}
[


iOS为此提供了UISearchController。以下是将搜索栏添加并约束到故事板中现有容器视图的代码,以及设置tableViewController以在用户开始键入时显示的代码

    locationSearchResultTableViewController = storyboard?.instantiateViewController(withIdentifier: String(describing: LocationSearchResultTableViewController.self)) as! LocationSearchResultTableViewController
    locationSearchResultTableViewController.delegate = self
    searchController = UISearchController(searchResultsController: locationSearchResultTableViewController)
    searchController.searchResultsUpdater = locationSearchResultTableViewController
    searchController.dimsBackgroundDuringPresentation = false
    searchController.definesPresentationContext = true
    searchContainer.addSubview(searchController.searchBar)
    searchController.searchBar.translatesAutoresizingMaskIntoConstraints = false
    let attributes: [NSLayoutAttribute] = [.top, .bottom, .left, .right]
    attributes.forEach { NSLayoutConstraint(item: self.searchController.searchBar, attribute: $0, relatedBy: .equal, toItem: searchController.searchBar.superview, attribute: $0, multiplier: 1, constant: 0)}

我找到了一个简单的解决方案。你只需在顶部有一个搜索栏,然后创建第二个UIView并将其放在搜索栏下面。然后连接新的UIView,如

@IBOutlet weak var view2: UIView!
然后,当涉及到框架部分时,您只需这样做

Popup.view.frame = self.view2.frame

也就是说,当您单击搜索栏时,View2将作为一个弹出窗口,覆盖搜索栏下方的所有内容。

谢谢您提供的代码,我现在将把它合并到