Search code examples
iosswiftuitableviewuisearchbaraddsubview

IOS swift How can I get my SubView to appear below my SearchBar


I have a UISearchBar embedded within a form so what I am trying to do is once someone clicks on the SearchBar I want a TableView to popup as a subView . I have that working correctly however I would like the SubView to appear under the SearchBar so that Users can keep searching, my SubView takes over the entire page . Here is how I am trying to get the functionality to work: Trulia App . Notice the searchBar with the corresponding View now if you click on the SearchBar a subView appears right below the SearchBar . As stated before for me everything is working except that the subView takes over the whole page and does not appear underneath the SearchBar. This is my code , the third image is my storyBoard below and this is my code

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

[Main Page[1]

SubView

enter image description here


Solution

  • I found an easy solution to my problem. You simply have a SearchBar at the top then you create a second UIView and put it underneath your SearchBar. Then you connect that new UIView such as

    @IBOutlet weak var view2: UIView!
    

    then when it comes to the frame portion you simply do this

    Popup.view.frame = self.view2.frame
    

    That is it when you click on SearchBar the View2 will act as a popup and cover everything underneath the searchBar .