UINavigationController 实例化后 UISearchbar 不可见

UISearchbar not visible after UINavigationController is instantiated

我有一个 UISearchBar 当我点击搜索按钮时加载。因此,我有 func searchBarSearchButtonClicked(searchBar: UISearchBar)func searchBarCancelButtonClicked(searchBar: UISearchBar) 委托方法。然而,问题是,当我转到另一个嵌入 UINavigationController 的视图控制器,然后返回带有 search-bar 的页面并单击搜索按钮时,整个标题视图不会不要出现。这是我目前的代码:

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

        }
}

这是在点击 pin 注释并转到 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
            }
        }
    }

为清楚起见,这里有一张 gif 图像:

确保 navigationItem.titleView 设置正确。您正在将 titleView 设置为 nil