UISearchBar displaysSearchBarInNavigationBar

2019-06-14 11:23发布

问题:

I want to replicate the functionality of the Twitter app where the search bar slides in to the navigation bar.

However i have run into a problem. If i don't add

[self.searchDisplayController setDisplaysSearchBarInNavigationBar:YES];

into viewDidLoad the bar will not appear. I have a search button in the righthand navigation item when i click it i call the above method. Is there anything i can do to ensure the search bar gets drawn

Thanks

Jonathan

回答1:

You can add search bar on navigation bar....

initially in your viewDidLoad:-

mySearchBar.frame = CGRectMake(0.0, -80.0, 320.0, 44.0);
[self.navigationController.navigationBar addSubview:mySearchBar];

in your action method

mySearchBar.frame = CGRectMake(0.0, 0, 320, 44);

in viewWillDisappear

[mySearchBar removeFromSuperview];