How to add search bar to navigation bar for iphone?

Possible duplicate:
How to add serach bar to navigation bar for iPhone

This is the code to display the search bar, but I want to put it in the navigation bar

sBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0,10,320,30)];

sBar.delegate = self;

[self.view addSubview:sBar];
//[self.navigationItem.rightBarButtonItem addSubview:sBar];
//self.navigationItem.rightBarButtonItem=sBar;

is there any way?

+5
source share
3 answers

Add the search bar as a title to the navigation bar. This sample code also includes a fix for the add-on that you get by adding it as a header.

UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(-5.0, 0.0, 320.0, 44.0)];
searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
UIView *searchBarView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 310.0, 44.0)];
searchBarView.autoresizingMask = 0;
searchBar.delegate = self;
[searchBarView addSubview:searchBar];
self.navigationItem.titleView = searchBarView;
+24
source

You can assign any representation of the titleView property of the navigation item. You can do it this way.

self.navigationItem.titleView = searchBar ;

, UIView, self.navigationItem.titleView.

+5

. UINavigationController NavigationBar.

, :

, . , titleView .

+1
source

All Articles