Varun Varahabotla
Varun Varahabotla

Reputation: 548

UISearchbar not visible after UINavigationController is instantiated

I have a UISearchBar that is loaded when I click on a searchButton. Accordingly, I have the func searchBarSearchButtonClicked(searchBar: UISearchBar) and func searchBarCancelButtonClicked(searchBar: UISearchBar) delegate methods. However the issue is that when I segue to another view controller that is embedded with a UINavigationController and I go back to the page with the search-bar and I click on the search button, the whole title view doesn't appear. Here is my code currently:

func searchbarPopulate() {
        tempSearchBar = searchButton
        searchBar2.delegate = self
        searchBar2.searchBarStyle = UISearchBarStyle.Minimal
        searchBar2.showsCancelButton = true
    }

    @IBAction func tapSearchButon(sender: AnyObject) {
        self.viewDidLoad()
        searchbarPopulate()
        self.navigationItem.rightBarButtonItem = nil
        navigationItem.titleView = searchBar2
        searchBar2.alpha = 0
        navigationItem.setLeftBarButtonItem(nil, animated: true)
        UIView.animateWithDuration(0.5, animations: {
//            self.searchBar2.alpha = 1
            }, completion: { finished in
                self.searchBar2 .becomeFirstResponder()
                self.searchBar2.alpha = 1
            })
    }

    func searchBarSearchButtonClicked(searchBar: UISearchBar) {
        attractionsMap.removeAnnotations(attractionsMap.annotations);
        performYelpSearch(searchBar.text)
        searchBarCancelButtonClicked(searchBar)
    }
    func searchBarCancelButtonClicked(searchBar: UISearchBar) {
        UIView.animateWithDuration(0.5, animations: { () -> Void in
            searchBar.alpha = 0
            }) { (completed) -> Void in
//                self.navigationItem.titleView = "Attractions"
                // Make Title View of Attractions in Sketch
                self.navigationItem.rightBarButtonItem = self.searchButton

        }
}

and this is when the pin annotation is tapped and segues to the attractionsDetailVC:

func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {
    if (control == view.rightCalloutAccessoryView) {
        let selectedLocation = view.annotation;
        let selectedCoordinate = view.annotation.coordinate;
        var latitude = selectedCoordinate.latitude
        var longitude = selectedCoordinate.longitude
        var location:CLLocation = CLLocation(latitude: latitude, longitude: longitude)
        let businessPlacemark = MKPlacemark(coordinate: selectedCoordinate, addressDictionary: nil)
        indicatedMapItem = selectedCoordinate;
        let resturantMock:Resturant = Resturant(dictionary: resultQueryDictionary)
        let dataArray = resultQueryDictionary["businesses"] as! NSArray
        var foundDisplayAddress:String = "Address not found"
        for business in dataArray {
            let obj = business as! NSDictionary
            var yelpBusinessMock: YelpBusiness = YelpBusiness(dictionary: obj)
            if yelpBusinessMock.latitude == view.annotation.coordinate.latitude {
                if yelpBusinessMock.longitude == view.annotation.coordinate.longitude {
                    attractionDict = obj;
                    foundDisplayAddress = yelpBusinessMock.displayAddress
                    businessMock = Business(dictionary: obj)
                }
            }
        }
        attractionLocationString = foundDisplayAddress
        performSegueWithIdentifier("attractionToDetail", sender: view);
    }
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "attractionToDetail" {
        if let annotation = (sender as? MKAnnotationView)?.annotation {
            if let ivc = segue.destinationViewController as? AttractionsDetailViewController {
                searchBarCancelButtonClicked(searchBar2)
                ivc.attractionLocation = self.indicatedMapItem
                ivc.attractionDetailAddressString = self.attractionLocationString
                ivc.currentBusiness = self.attractionDict
                ivc.businessToUse = self.businessMock
            }
        }
    }

Here is a gif of what happens for clarity:
enter image description here

Upvotes: 1

Views: 454

Answers (1)

kebabTiger
kebabTiger

Reputation: 652

Make sure that you are setting the navigationItem.titleView correctly. You're setting the titleView to nil

Upvotes: 1

Related Questions