Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add search bar in navigation bar for iphone? [duplicate]

Tags:

iphone

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?

like image 878
ram Avatar asked Jul 28 '10 09:07

ram


People also ask

Can you change the navigation bar on Iphone?

Change the Bar StyleA user changes the navigation bar's style, or UIBarStyle , by tapping the “Style” button to the left of the main page. This button opens an action sheet where users can change the background's appearance to default, black-opaque, or black- translucent.

How do you change the title of a navigation?

Use navigationBarTitle(_:) to set the title of the navigation bar. This modifier only takes effect when this view is inside of and visible within a NavigationView .


3 Answers

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;
like image 134
Bjarne Mogstad Avatar answered Nov 07 '22 11:11

Bjarne Mogstad


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 .

like image 37
Bharat Jagtap Avatar answered Nov 07 '22 11:11

Bharat Jagtap


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.

like image 1
willcodejavaforfood Avatar answered Nov 07 '22 09:11

willcodejavaforfood