将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]; self .navigationController.navigationBar.tintColor = [UIColor redColor]; UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"艺人" style:UIBarButtonItemStyleBordered target:self action:@selector (showSelect)]; self .navigationItem.leftBarButtonItem = leftItem; _searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake (0.0 f,0.0 f,255.0 f,44.0 f)]; _searchBar.delegate = self ; [_searchBar setTintColor:[UIColor redColor]]; [_searchBar setPlaceholder:@"输入艺人名字" ]; UIView *searchView = [[UIView alloc]initWithFrame:CGRectMake (0 , 0 , 768 , 44 )]; searchView.backgroundColor = [UIColor blueColor]; [searchView addSubview:_searchBar]; self .navigationItem.titleView = searchView; 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.0 f,0.0 f,255.0 f,44.0 f)];
那么解决的办法就很简单了,只要你修改一个坐标和宽度,或者高度,你就可以任意的设置了。