在UISearchDisplayController中添加UISegmentedControl

我想在searchBar下面和UISearchDisplayController的TableView上面添加UISegmentedControl 。 目前, UISearchDisplayController仅在其SearchBar下显示其tableView

但我想在SearchBar下面添加一个UISegmentedControl ,以便我在SearchBar上面有SearchBar ,在SearchBar之后我有一个UISegmentedControl ,在UISegmentedControl下面我有UITableView 。 这是使用UISearchDisplayController做到这一点的任何方式,或者我必须创建自己的SearchDisplayController ,它有自己的SearchBarUISegmentedControlTableView

有什么建议吗? 谢谢

通过添加scop标题启用ShowScopeBar并添加尽可能多的段,并通过以下方法处理它们

按代码

 UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; [searchBar setShowsScopeBar:YES]; [searchBar setScopeButtonTitles:@[@"button1",@"button2"]]; [[self view] addSubview:searchBar]; 

通过XIB

在此处输入图像描述

委托方法来处理范围按钮操作

 - (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope{ } 

在表格中创建一个视图

 UIView *viewsearch=[[UIView alloc]initWithFrame:CGRectMake(0,-10, 320,83)]; [self.tblname addSubview:viewsearch]; [self.view addGestureRecognizer:revealController.panGestureRecognizer] UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,5, 320, 40)]; searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth; searchBar.delegate=self; [searchBar setBarTintColor:[UIColor colorWithRed:(249/255.0) green:(9/255.0) blue:(99/255.0) alpha:1]]; searchBar.tintColor = [UIColor whiteColor]; searchBar.placeholder = @"Search items eg jacket"; 

在该视图中添加搜索栏视图。

  [viewsearch addSubview:searchBar]; searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth; 

在此searchview中使用UISegmentedControl。

  NSArray *itemArray = [NSArray arrayWithObjects: @"General", @"Near me", nil]; UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray]; segmentedControl.frame = CGRectMake(50,53, 225, 22); segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain; [segmentedControl addTarget:self action:@selector(MySegmentControlAction:) forControlEvents: UIControlEventValueChanged]; segmentedControl.selectedSegmentIndex = 0; segmentedControl.tintColor = [UIColor colorWithRed:(249/255.0) green:(10/255.0) blue:(99/255.0) alpha:1]; segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin; [viewsearch addSubview:segmentedControl]; 

我找到了我的问题的解决方案,我添加了UISegmentedControl作为UISearchDisplayController的TableView的头。 这样看起来我刚刚在UISearchDisplayController的searchBar下添加了一个UISegmentedControl。

谢谢每一个试图帮助的人。