如何在iPhone的导航栏中添加搜索栏? [英] How to add search bar in navigation bar for iphone?
本文介绍了如何在iPhone的导航栏中添加搜索栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
如何在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;
有什么办法吗?
推荐答案
将搜索栏添加为导航栏的标题视图。此代码示例还包括通过将其添加为标题视图来获得额外填充的修复。
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;
这篇关于如何在iPhone的导航栏中添加搜索栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文