Swift Everyday
Swift Everyday

Reputation: 281

Swift: UISearchController not in the window hierachy

I added a search bar on a UITableViewController but I am getting an error every time I click the search bar. The view turns black as well..

Warning: Attempt to present OWSearchController on FindViewController: whose view is not in the window hierarchy!

enter image description here

I tried configuring the SearchController both in the viewDidLoad and viewDidAppear and its the same.

class FindViewController: UITableViewController {

    var searchController: OWSearchController!

    override func viewDidAppear(animated: Bool) {
        self.configureSearchController()
    }

    func configureSearchController() {

        // Create the search controller and make it perform the results updating.
        searchController = OWSearchController(searchResultsController: self)
        searchController.hidesNavigationBarDuringPresentation = false


        // Configure the search controller's search bar.
        searchController.searchBar.searchBarStyle = .Minimal
        searchController.searchBar.placeholder = NSLocalizedString("Search", comment: "")

        // Include the search bar within the navigation bar.
        navigationItem.titleView = searchController.searchBar
        definesPresentationContext = true
    }
}

May someone please help me out on what is wrong with it? Thank you so much

Upvotes: 0

Views: 247

Answers (1)

Andriy Zymenko
Andriy Zymenko

Reputation: 143

This is a working code copy from my project

func displayContentController(content: DrawingViewController) {
    self.addChildViewController(content)
    let drawingFrame = AVMakeRectWithAspectRatioInsideRect(imageView.image!.size, imageView.bounds);
    content.view.frame = drawingFrame;
    imageView.addSubview(content.view); //image view is a self.view child, you may just use self.view?.addSubview(content.view)
    //content.drawingView.reset()
    content.didMoveToParentViewController(self);
}

func hideContentController(content: DrawingViewController) {
    content.willMoveToParentViewController(nil);
    content.view.removeFromSuperview();
    content.removeFromParentViewController();
}

Upvotes: 0

Related Questions