I have two views in my view controller first one is uitableview and the second one is bottom fixed UIView. I have autolayout constraint set up for both views, the table is at top and the second one is fixed at bottom, in the table view I am adding search bar. On the view load everything renders fine but as soon as the search bar is made active, the first two cells slip under the navigation bar. If i remove the Bottom view and bottom constraint everything works fine.
Here is the sample code im running
self.tableView = [[UITableView alloc] initWithFrame:CGRectZero
style:UITableViewStylePlain];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 80;
self.tableView.tableFooterView = [UIView new];
[self.view addSubview:self.tableView];
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.trailing.top.mas_equalTo(self.view);
make.bottom.equalTo(self.bottomToolBar.mas_top);
}];
self.definesPresentationContext = YES;
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
// Configure the search bar with scope buttons and add it to the table view header
self.searchController.searchBar.scopeButtonTitles = #[#"Country",#"Capital"];
self.searchController.searchBar.delegate = self;
self.tableView.tableHeaderView = self.searchController.searchBar;
self.definesPresentationContext = YES;
[self.searchController.searchBar sizeToFit];
I have two ways of searching for friends within my app. One of them is a snapchat similar interface where the added friends are shown and possibility of adding a friend is shown with a + sign. I tried adding a search bar somewhere within the area programmatically but got no results of nothing displaying. The code was implemented on my FriendsViewController that I am using and this is the current code I have:
-(void)initialiseSearchBar
{
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
UITableViewController *searchResultsController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
searchResultsController.tableView.dataSource = self;
searchResultsController.tableView.delegate = self;
searchResultsController.tableView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.7];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
self.tableView.tableHeaderView = self.searchController.searchBar;
self.searchController.searchResultsUpdater = self;
[self.tableView setContentOffset:CGPointMake(0, 0)];
self.edgesForExtendedLayout = UIRectEdgeNone;
self.searchResults = [NSMutableArray array];
}
Any idea what I have done wrong or should fix? Also I am planning on releasing with iOS 7 compatibility and up all the way until 8.1, I have worries that the terms might be different or there is something depreciated that I do not know of.
You've allocated it but you haven't added it to the view itself unless it was made in storyboard (which you clearly state you didn't do) see my answer on another question... you have to add it as a subview somehow:
Adding A Search Bar
I have gone through all the documentation I can find, but I just can't figure out how to solve this issue. When I click on the search text box, the lightbox comes up, but does not cover the whole screen (looks like it is off by ~64dp). Here are a couple of screenshots.
I have my search stuff inside of a tableViewController
Before:
After:
Has anyone had experience fixing this issue?
Here is how I am setting everything up:
self.searchBar = [[UISearchBar alloc] init];
self.searchBar.barTintColor = [UIColor whiteColor];
self.searchBar.delegate = self;
self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
self.searchController.delegate = self;
self.searchController.searchResultsDelegate = self;
self.searchController.displaysSearchBarInNavigationBar = YES;
Turns out I had a self.edgesForExtendedLayout = UIRectEdgeNone; in the viewDidLoad, and that messed EVERYTHING up...
I want to add a UISearchBar to an UIView instead of an UIViewController, the problem is that the init method of UISearchDisplayController needs an UIViewController as contentsController:.
I can see the UISearchBar correctly displayed in the view but if I click in it, the bar disappears. Here is my code I'm currently using:
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
// this was my approach, but this DOES NOT work
UIViewController *results = [[UIViewController alloc] init];
results.view.userInteractionEnabled = NO;
[self insertSubview:results.view aboveSubview:self.menuList];
self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:results];
self.searchController.delegate = self;
self.searchController.searchResultsDataSource = self;
self.searchController.searchResultsDelegate = self;
self.tableView.tableHeaderView = self.searchBar;
Before:
After clicking into search bar:
In swift,
I use hidesNavigationBarDuringPresentation to solve this problem.
This problem seems like after the search bar is clicked, it will automatically hide the bar. And it will be fine after you click outside the view.
I add this line:
self.searchController?.hidesNavigationBarDuringPresentation = false;
Hope it works
I have a UIViewController in which I want to show a tableview with the serchBar.
//viewDidLoad
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,
0,
SCREEN_WIDTH(),
SCREEN_HEIGHT())
style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
// adding uisearch bar
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
_tableView.tableHeaderView = searchBar;
//
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
The issue happens when I click inside the uisearch bar so that the animation starts and it looks like it has a 20px unwanted offset.
In your Storyboard, select the problematic controller, look at the Attributes tab and try to edit these settings:
Under Top Bars
Under Opaque Bars
I've solved a similar problem by unflagging these settings.
I found what is causing this issue. Seems that the animation gets messed up when you set navigationBar.translucent to NO. If you make your navigationBar translucent, everything should work fine, but this is definitely not an ideal solution. I'm going to try and find a workaround.
UITableView *_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,
64,
self.view.frame.size.width,
self.view.frame.size.height)
style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
// adding uisearch bar
UISearchBar* searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
_tableView.tableHeaderView = searchBar;
//
UISearchDisplayController* searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
and I just embade my controller with UINavigationcontroller and its working quite well..
You can cancel animation by subclassing UISearchDisplayController and adding this:
- (void)setActive:(BOOL)visible animated:(BOOL)animated
{
if(self.active == visible) return;
[self.searchContentsController.navigationController setNavigationBarHidden:YES animated:NO];
[super setActive:visible animated:animated];
[self.searchContentsController.navigationController setNavigationBarHidden:NO animated:NO];
if (visible) {
[self.searchBar becomeFirstResponder];
} else {
[self.searchBar resignFirstResponder];
}
}
codyko gave me an idea. It was the because the navigation bar was no translucent. So I set it to translucent on this view controller and rest when I left with the following code:
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
self.navigationController.navigationBar.translucent = NO;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.navigationController.navigationBar.translucent = YES;
}
Now that left the navigation bar on this controller slightly faded, so I added a UIView the same color as my navbar just behind it to make it look opaque. I know it isn't perfect but it works nicely.
As a reminder for anyone with similar issues. I needed to add this line that fixed things:
self.edgesForExtendedLayout = UIRectEdgeNone;
Why are you creating the searchBar programmatically instead of in the StoryBoard ?
I'm currently using searchBar, added in storyboard and it's work fine ( I have to change the contentOffset )
I have applied your code,, It works fine for me,, Just hide you navigation bar and start the search bar from y = 20, instead of y = 0;