将UISearchBar放在UINavigationBar之上,实现的过程如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.navigationController.navigationBar.tintColor = [UIColor redColor];

//添加左侧的item
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"艺人"
style:UIBarButtonItemStyleBordered target:self
action:@selector(showSelect)];
self.navigationItem.leftBarButtonItem = leftItem;


//导航条的搜索条
_searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0.0f,0.0f,255.0f,44.0f)];
_searchBar.delegate = self;
[_searchBar setTintColor:[UIColor redColor]];
[_searchBar setPlaceholder:@"输入艺人名字"];

//将搜索条放在一个UIView上
UIView *searchView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 768, 44)];
searchView.backgroundColor = [UIColor blueColor];
[searchView addSubview:_searchBar];
self.navigationItem.titleView = searchView;

//设置tableview的代理
self.dataTable.delegate = self;
self.dataTable.dataSource = self;

//加载数据
NSString *path=[[NSBundle mainBundle] pathForResource:@"category" ofType:@"plist"];
self.dataDic = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
}

如果说你的searchbar的位置不对,那么好的我这里高速你应该如何去调整吧:

请你注意一下这条语句:

1
_searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0.0f,0.0f,255.0f,44.0f)];

那么解决的办法就很简单了,只要你修改一个坐标和宽度,或者高度,你就可以任意的设置了。