6

I'm creating a UISearchViewController and I wish to put the UISearchBar in the navigation controller's navigation item. Here's how I did it with no results:

self.searchBar = [UISearchBar new];
self.searchBar.showsScopeBar = YES;
self.searchBar.scopeButtonTitles = @[@"Users", @"Hashtags"];
self.searchBar.delegate = self;

self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
self.searchController.delegate = self;
self.searchController.searchResultsDataSource = self;
self.searchController.searchResultsDelegate = self;
self.searchController.displaysSearchBarInNavigationBar = YES;

I also tried:

self.searchDisplayController.searchBar.searchBarStyle = UISearchBarStyleMinimal;
self.searchDisplayController.searchBar.scopeButtonTitles = @[@"Users", @"Hashtags"];
self.searchDisplayController.searchBar.showsScopeBar = YES;

Am I missing out anything or doing something wrong here?

Edit: This and this is exactly what I'm aiming for.

Ryan
  • 490
  • 5
  • 14
  • Did you find a solution for this ? I am struck in the same problem – Kong Hantrakool Apr 10 '14 at 08:53
  • @KongHantrakool I haven't found a solution for this yet. In the mean time, I am using a UISegmentedControl in the navigation bar to do the job of the scope bar instead. – Ryan Apr 10 '14 at 13:47

4 Answers4

1

Try with following code

UISearchBar* searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
searchBar.barStyle = UIBarStyleBlack;
searchBar.scopeButtonTitles = [NSArray arrayWithObjects:@"Users", @"Hashtags", nil];
searchBar.showsScopeBar = YES;
self.searchBar.delegate = self;
iPatel
  • 46,010
  • 16
  • 115
  • 137
  • 1
    Unfortunately, the scope bar is still not showing. Just the searchbar in the navigation bar. – Ryan Feb 26 '14 at 15:38
1

The scope bar will only appear when the search bar is active. The search display controller is hiding the search bars scope bar.

If you want the scope bar always available, you'll have to do a little hack work. Maybe one of these will help.

How to have UISearchBar scope bar always visible?

IOS7 : uisearchdisplaycontroller always show scope bar

EDIT:

Have you tried something in this direction? Assuming you have added the search bar to the navigation bar's titleView.

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    CGRect rect = self.navigationItem.titleView.superview.frame;
    rect.size.height = 88.0f;
    self.navigationItem.titleView.superview.frame = rect;
}
Community
  • 1
  • 1
cnotethegr8
  • 7,342
  • 8
  • 68
  • 104
  • Hi, I have no problems showing scope bars when the bars are in self.view or tableView.headerView. The scope bars are just not showing whenever the searchbar is in a navigation bar, either by adding it to a view then setting that view as the navigation bar's title view, or by "self.searchController.displaysSearchBarInNavigationBar = YES;". – Ryan Apr 02 '14 at 00:45
  • Ahh, it sounds like the scope bar is being clipped from the navigation bar. Have you tried extending the height of the navigation bar? Possibly do this work in the view controllers -viewDidLayoutSubviews. – cnotethegr8 Apr 02 '14 at 07:14
  • Did you find a solution for this ? I am struck in the same problem – Kong Hantrakool Apr 10 '14 at 08:52
0

Try adjusting the searchBar frame:-

UISearchBar* searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake:(0,0,320,88)];
Mohd Iftekhar Qurashi
  • 2,385
  • 3
  • 32
  • 44
0

I set "Use Auto Layout" from xip inspector -> interface builder

Thats working perfectly with me

mohammad alabid
  • 444
  • 7
  • 21