Question

I was wondering is there any way to implement such UI features. As we know when we dragged in a search bar and searchDisplay control in xcode 5, we are be given a set of search controls including a search bar and a searchDisplayController.

Instead of using the native search bar, I want to use a button to trigger the search display control like that one in the latest facebook app.

I have tried to delete the search bar. But the display control failed directly. I have tried to insert the search bar inside the tableView (Which I don't know its correct or not)

enter image description here

- (void)viewDidLoad
{
   [self hideSearchBar];
}

- (void)hideSearchBar
{
    self.UISearchBar.hidden = YES;
     self.UISearchBar.bounds = CGRectMake(0, 0, 0, 0);
}

- (IBAction)SearchClicked:(UIButton *)sender {
    [self.searchDisplayController setActive:YES animated:NO];
    self.UISearchBar.hidden = NO;
}

- (void) searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller
{
   [self hideSearchBar];
}

But the problem with it is:

When I cancel the search the scroll view is not return to its previous state. (There is a gap space where the hidden searchBar sits).

Instead of seeing this:

enter image description here

I want to see this when the cancel button is clicked in the search view.

enter image description here

How can I address this issue?

Can any body provide me with a better solution?

Thanks

Was it helpful?

Solution 3

So finally instead of dragging a searchBar control into the storyboard. I'm creating and destroying the searchBarControl on the fly.

-(void)initialiseSearch
{
    //Start
    searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];

    searchBar.delegate = self;
    searchBar.showsCancelButton = YES;
    searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];

    searchDisplayController.delegate = self;
    searchDisplayController.searchResultsDataSource = self;
    searchDisplayController.searchResultsDelegate = self;

    [self.view addSubview:searchBar];

    searchBar.hidden = YES;
    [searchDisplayController setActive:NO animated:NO];

    //    [self.searchDisplayController.searchResultsTableView registerNib:[UINib nibWithNibName:@"CustomSearch" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"search"];
    //End
}

The method is called in the view did load.

OTHER TIPS

So simple!

 -(void) searchBarCancelButtonClicked:(UISearchBar *)searchBar{
        [self.searchDisplayController setActive:NO animated:YES];
        self.navigationController popToViewController:UIViewControllerA animated:YES;
    }

Create a CGPoint based on the scrollview's original center. In viewDidLoad() :

self.originalCenter = scrollView.center;

And when your user closes the searchbar:

- (void)hideSearchBar
    scrollView.center = self.originalCenter;

Where scrollView is whatever you've called your UIScrollView.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top