Search code examples
iosuitableviewuisearchcontrolleriphone-8-plus

Scope buttons hiding behind the UITableview


I am creating a search screen using UISearchController, in which I have to show three scope buttons.I have put search bar programmatically.But somehow the scope buttons are hiding behind the UITableView.

- (void)viewDidLoad {
    [super viewDidLoad];
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.searchResultsUpdater = self;
    self.searchController.dimsBackgroundDuringPresentation = NO;
    self.searchController.searchBar.delegate = self;
    self.tblFoundList.tableHeaderView = self.searchController.searchBar;
    self.definesPresentationContext = YES;
    [[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setDefaultTextAttributes:@{NSForegroundColorAttributeName: [UIColor blackColor]}];
    [[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setAttributedPlaceholder:[[NSAttributedString alloc] initWithString:@"Search by" attributes:@{NSForegroundColorAttributeName: [UIColor lightGrayColor]}]];
    self.searchController.searchBar.tintColor = [UIColor purpleColor];
    self.searchController.searchBar.barTintColor = [UIColor groupTableViewBackgroundColor];
    self.searchController.searchBar.scopeButtonTitles = @[@"A",@"B", @"C"];
    [self.searchController.searchBar sizeToFit];
}

Scope buttons hiding behind the UITableview

Could anyone help in this? Thank you.


Solution

  • As I mentioned earlier I had created UISearchController programmatically, so the UITableview in the image is UISearchController's UITableview, that is why nothing was affecting it.

    So I got help from

    UISearchController Search Bar Position Drops 64 points

    and

    UISearchBar presented by UISearchController in table header view animates too far when active

    - (UIBarPosition)positionForBar:(id<UIBarPositioning>)bar
        {
          if (bar == self.searchController.searchBar) {
            return UIBarPositionTopAttached;
          }
          else { // Handle other cases
            return UIBarPositionAny;
          }
        }
    

    On adding following method the search bar got shifted up and the scope buttons were visible.

    But thank you all for helping out.