gpt4 book ai didi

iphone - 如何在iPhone导航栏中添加搜索栏?

转载 作者:行者123 更新时间:2023-12-03 18:48:48 24 4
gpt4 key购买 nike

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

这是显示搜索栏的代码,但我想将其放在导航栏中

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;

有什么办法吗?

最佳答案

将搜索栏作为标题 View 添加到导航栏。此代码示例还修复了通过将其添加为标题 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 - 如何在iPhone导航栏中添加搜索栏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3351464/

24 4 0