开发者

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

开发者 https://www.devze.com 2023-01-09 09:05 出处:网络
This question already has an answer here: 开发者_运维知识库 Closed 10 years ago. Possible Duplicate:
This question already has an answer here: 开发者_运维知识库 Closed 10 years ago.

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?


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;


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 .


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号