5

Possible Duplicate:
How to add serach bar in navigation bar for iPhone

This is the code to display search bar but i want to put this in 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?

Community
  • 1
  • 1
ram
  • 469
  • 3
  • 5
  • 13

3 Answers3

24

Add the search bar as title view to the navigation bar. This code example also includes a fix for the extra padding you get by adding it as a title view.

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;
Undo
  • 25,519
  • 37
  • 106
  • 129
Bjarne Mogstad
  • 1,154
  • 13
  • 18
  • i did same thing but earler i made and IBoutlet of serachbar that was wroking fine but that was scrolling so using your code my serach function is not working ?? how to solve that i think i needt to omit delgate from Iboutlet what you suggest?? i am geetin a waring if i use this searchBara.delegate = self; rootviewCnt doest not impalement UIseachbar delegate protocol – ram Jul 28 '10 at 10:08
  • I have the problem that the results are shown in a table view embedded in a popover, instead of in the frame of the original table view. Any solution to this? – Norbert Oct 05 '13 at 08:55
  • This seems like a good answer. Where would I place this code? If I copy and paste it into a method called `addSearchBarToNavigationBar`. Where would I call the method? I am using a TableViewController – learner Jul 17 '14 at 17:00
5

you can assign any view to the titleView property of the navigation item . You can do it this way .

self.navigationItem.titleView = searchBar ;

This should work out or not then adding search bar to the UIView instance and then assigning this view to self.navigationItem.titleView will do .

iDev
  • 23,310
  • 7
  • 60
  • 85
Bharat Jagtap
  • 1,692
  • 2
  • 22
  • 35
1

You can provide a custom view for the title section of a navigation bar. Take a look at the UINavigationController and especially the Updating the NavigationBar section.

The navigation controller updates the middle of the navigation bar as follows:

If the new top-level view controller has a custom title view, the navigation bar displays that view in place of the default title view. To specify a custom title view, set the titleView property of the view controller’s navigation item.

willcodejavaforfood
  • 43,223
  • 17
  • 81
  • 111
  • i did this self.navigationItem.titleView = sBar; its working but my search () is not working i did this bsBar.delegate = self; now am i missing anything else – ram Jul 28 '10 at 10:04
  • On iPad With this the results are shown in a popover instead of in the original table view area. Any solution to this? – Norbert Oct 05 '13 at 08:56