Mahbub Ahmed
Mahbub Ahmed

Reputation: 196

UISearchController search bar appears but not working/triggering

In my view controller I am adding a search bar programmatically and it appears exactly the way i want but when i click on it do nothing i can't write/type anything on the search bar, And there is no error on the log. I added search bar in another view controller same way,it does works but in that particular it doesn't work. I am pushing this view controller from previous view controller so it has a navigation back button on the left (check the image) it works too.

In the view controller there is a TableView and a CollectionView i want to add the search for the table view. I was wondering if there is anything to do with having both of this in the same view controller.

Code to add search bar

- (void)viewDidLoad {
   //other stuff on view controller
   [self setSearchView];
}

-(void)setSearchView{
    //searchController is added as a property on .h file 
    //and all the delegate are also added
    //@property (strong, nonatomic) UISearchController *searchController;

    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.searchResultsUpdater= self;
    self.searchController.dimsBackgroundDuringPresentation = NO;
    self.searchController.searchBar.delegate = self;
    self.navigationController.toolbarHidden=YES;

    self.navigationItem.titleView=self.searchController.searchBar;

    self.searchController.hidesNavigationBarDuringPresentation = NO;
    self.definesPresentationContext = YES;
}

I am coming to this view controller from a previous view controller and pushing this view controller when a button is clicked .And i am using nib file
Button action

- (void)ButtonClicked {
   MYNextViewController *nextViewController = [[MYNextViewController alloc] initWithNibName:@"MYNibFileName" bundle:nil];

    // I also have a tab bar on the bottom which i am hiding for that view controller when pushing
    nextViewController .hidesBottomBarWhenPushed=YES;

    [self.navigationController pushViewController:nextViewController  animated:YES];
}

Image:
enter image description here

Any help will be greatly appreciated. Hope i made my self clear...

Upvotes: 0

Views: 2426

Answers (4)

Vineet Rai
Vineet Rai

Reputation: 78

For your particular case, Use this.

 self.searchController.obscuresBackgroundDuringPresentation = NO;

This will allow you to use the same tableview to interact with search result.

Upvotes: 0

vb11
vb11

Reputation: 253

I had a similar issue and tried the answers here, but it didnt work for me. Then stumbled upon this post https://forums.developer.apple.com/thread/122972

Basically for me disabling Automatically Sync Pasteboard worked. (Simulator -> Edit -> Automatically Sync Pasteboard)

Upvotes: 1

Mahbub Ahmed
Mahbub Ahmed

Reputation: 196

Finally i was able to solve this problem with some change in view hierarchy thanks to @Tj3n , but not sure if this is the correct solution but right now its working .. I also had to remove this line and add few more codes for other purpose

self.definesPresentationContext = YES; //removed...

also answer of that questions was helpful

Upvotes: 3

Vinay Kumar
Vinay Kumar

Reputation: 107

//Please use this code

  _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, self.navigationController.navigationBar.bounds.size.height)];
_searchBar.tintColor = [UIColor blueColor];

//[_searchBar setImage:[UIImage imageNamed:@"cross_icon"] forSearchBarIcon:UISearchBarIconBookmark state:UIControlStateNormal];
_searchBar.placeholder = @"Enter drug name";
//_searchBar.showsBookmarkButton = YES;
_searchBar.barTintColor = [UIColor clearColor];

UITextField *txfSearchField = [_searchBar valueForKey:@"_searchField"];
txfSearchField.backgroundColor = [UIColor whiteColor];
txfSearchField.tintColor = [UIColor blackColor];
txfSearchField.textColor = [UIColor blackColor];
_searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
_searchBar.delegate = self;
_searchBar.showsCancelButton = NO;
[_searchBar becomeFirstResponder];
[self.navigationController.navigationBar addSubview:_searchBar];

Upvotes: 0

Related Questions