Search code examples
iosiphoneuisearchbarcancel-buttonuisearchbardisplaycontrol

resize uisearchdisplaycontroller width when click cancel button


I want to change width of uisearchDisplaycontroller when click cancel. i add my code for reference...

mySearchBar = [[UISearchBar alloc]init];
[mySearchBar setDelegate:self];
resName_search = [[UISearchDisplayController alloc] initWithSearchBar:mySearchBar contentsController:self];
resName_search.delegate = self;
resName_search.searchResultsDataSource = self;
resName_search.searchResultsDelegate = self;
[self.view addSubview:resName_search.searchBar];
[resName_search.searchBar setFrame:CGRectMake(0.0, barOriginY+79.0, 106.0, 40.0)];

it displays good...but when i search item and then click cancel button... it does not resize ..

-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
  {
   btnCuisine.hidden =YES;
   btnService.hidden =YES;
   imgDropDwn1.hidden =YES;
   imgDropDwn2.hidden =YES;
   [resName_search.searchBar  setShowsCancelButton:YES animated:NO];
   [resName_search.searchBar setFrame:CGRectMake(0.0, barOriginY+79.0, 320.0, 40.0)];
   return YES;
  } 
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
  {
   NSLog(@"size changed 106.0");
   [resName_search.searchBar  setShowsCancelButton:NO animated:NO];
   [resName_search.searchBar setFrame:CGRectMake(0.0, barOriginY+79.0, 106.0, 40.0)];
   btnCuisine.hidden =NO;
   btnService.hidden =NO;
   imgDropDwn1.hidden =NO;
   imgDropDwn2.hidden =NO;
  }

i also add image link for that problem plz refer that solve my problem image Link


Solution

  • Just set self.searchDisplayController.searchBar.translatesAutoresizingMaskIntoConstraints to YES and setActive to NO. but translatesAutoresizingMaskIntoConstraints to YES is not important i think...

    -(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
    {
     NSLog(@"size changed 106.0");
     self.searchDisplayController.searchBar.translatesAutoresizingMaskIntoConstraints = YES;
     [self.searchDisplayController setActive:NO animated:YES];
     [resName_search.searchBar  setShowsCancelButton:NO animated:NO];
     [resName_search.searchBar setFrame:CGRectMake(0.0, barOriginY+79.0, 106.0, 40.0)];
    // mySearchBar.frame =CGRectMake(0.0, barOriginY+79.0, 106.0, 40.0);
    btnCuisine.hidden =NO;
    btnService.hidden =NO;
    imgDropDwn1.hidden =NO;
    imgDropDwn2.hidden =NO;
    }
    

    tap on Background of table while searching searchbar not resize So add searchDisplayControllerDidEndSearch delegate

    -(void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
    {
    NSLog(@"size changed 106.0");
    //    [resName_search.searchBar removeFromSuperview];
    self.searchDisplayController.searchBar.translatesAutoresizingMaskIntoConstraints = YES;
    [self.searchDisplayController setActive:NO animated:YES];
    [resName_search.searchBar  setShowsCancelButton:NO animated:NO];
    //    [self.view addSubview:resName_search.searchBar];
    [resName_search.searchBar setFrame:CGRectMake(0.0, barOriginY+79.0, 106.0, 40.0)];
    //    mySearchBar.frame =CGRectMake(0.0, barOriginY+79.0, 106.0, 40.0);
    btnCuisine.hidden =NO;
    btnService.hidden =NO;
    imgDropDwn1.hidden =NO;
    imgDropDwn2.hidden =NO;
    }