tags:

views:

77

answers:

2

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?

A: 

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
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
A: 

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;
Bjarne Mogstad
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