【问题标题】:IOS swift How can I get my SubView to appear below my SearchBarIOS swift如何让我的子视图出现在我的搜索栏下方
【发布时间】:2016-11-11 18:46:43
【问题描述】:

我在表单中嵌入了 UISearchBar,所以我想要做的是一旦有人点击 SearchBar,我希望 TableView 作为 subView 弹出。我的工作正常,但是我希望 SubView 出现在 SearchBar 下,以便用户可以继续搜索,我的 SubView 接管整个页面。以下是我试图让该功能发挥作用的方法:Trulia App。 如果您单击 SearchBar,现在请注意具有相应 View 的 searchBar 一个 subView 出现在 SearchBar 的正下方。如前所述,一切正常,除了 subView 接管整个页面并且没有出现在 SearchBar 下方。这是我的代码,第三张图片是我的故事板,这是我的代码

       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 swift uitableview uisearchbar addsubview


    【解决方案1】:

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

    【讨论】:

    • 感谢您的代码,我现在将其合并到
    【解决方案2】:

    我找到了解决问题的简单方法。您只需在顶部有一个 SearchBar,然后创建第二个 UIView 并将其放在 SearchBar 下方。然后你连接那个新的 UIView 比如

    @IBOutlet weak var view2: UIView!
    

    那么当涉及到框架部分时,您只需这样做

    Popup.view.frame = self.view2.frame
    

    当您单击 SearchBar 时,View2 将充当弹出窗口并覆盖 searchBar 下方的所有内容。

    【讨论】:

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