When adding a UISearchController to a UITableView header view, it is impossible to set the background behind the UISearchBar.
The second picture is showing the view when dragging to the bottom.
The UITableView is set in the Storyboard without any headers.
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
// Setup appearance
self.searchController.searchBar.barStyle = UIBarStyleBlack;
self.searchController.searchBar.barTintColor = BLUE;
self.searchController.searchBar.tintColor = RED;
// Setup logic
self.searchController.searchBar.scopeButtonTitles = #[ NSLocalizedString(#"students", nil), NSLocalizedString(#"professors", nil), NSLocalizedString(#"rooms", nil) ];
self.searchController.searchBar.delegate = self;
self.searchController.searchResultsUpdater = self;
self.tableView.tableHeaderView = self.searchController.searchBar;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.definesPresentationContext = YES;
I tried adding the background to the storyboard, to the superview, the UITableView, and manually to the UISearchBar.
More code available here.
You can try this:
UIView* topview = [[UIView alloc] initWithFrame:CGRectMake(0, -480, self.view.frame.size.width, 480)];
topview.backgroundColor = BLUE;
[self.tableView addSubview:topview];
I have tested with a UITableViewController and it works fine.
Inspired from this answer
Related
Until iOS 11 was released, I had a search bar added to a UITableViewController. It is hidden under a navigation bar and is visible when user scrolls the table up. On iOS11, adjusting contentOffset or tableview.bound won't hide the search bar properly, sometimes it does work and sometimes it hides the first row, too. So, I've decided to move the search bar to the navigation bar. But, for some reason, I don't see it anywhere. Here's my code at viewDidLoad
YearTableViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
if (#available(iOS 11.0, *)) {
//iOS11 -> on navigation bar
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.delegate = self;
self.searchController.searchBar.delegate = self;
self.searchController.searchBar.barStyle = UISearchBarStyleDefault;
self.searchController.searchBar.showsCancelButton = NO;
[self.searchController.searchBar sizeToFit];
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.searchController.dimsBackgroundDuringPresentation = YES;
[self.navigationItem setSearchController:self.searchController];
[self.navigationController.navigationBar setPrefersLargeTitles:NO];
[self.navigationItem setLargeTitleDisplayMode:UINavigationItemLargeTitleDisplayModeNever];
self.definesPresentationContext = YES;
} else {
//iOS10 or previous -> on table view
UITableViewController *tableViewControllerForSearch = [[UITableViewController alloc]initWithStyle:UITableViewStylePlain];
tableViewControllerForSearch.tableView.dataSource = self;
tableViewControllerForSearch.tableView.delegate = self;
tableViewControllerForSearch.tableView.estimatedRowHeight = 40;
tableViewControllerForSearch.tableView.rowHeight = UITableViewAutomaticDimension;
self.searchController = [[UISearchController alloc] initWithSearchResultsController:tableViewControllerForSearch];
self.searchController.searchResultsUpdater = self;
self.searchController.delegate = self;
self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);
self.tableView.tableHeaderView = self.searchController.searchBar;
self.definesPresentationContext = YES;
// No border between cells
tableViewControllerForSearch.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
}
EDITED: 2017/10/20
As suggested, I've renamed and simplified the structure.
I have the following structure on storyboard.
UINavigationController: Initial view controller
YearPageViewController: Inside, it has YearTableViewController
On iOS10, it works fine. On iOS 11, I don't see the searchBar.
Thanks for your input.
What about adding
self.navigationItem.hidesSearchBarWhenScrolling = NO;
Obviously the search bar is hidden by default and only is to be seen when scrolling.
Hope will helpful to you On iOS11
if #available(iOS 11.0, *) {
self.navigationItem.hidesSearchBarWhenScrolling = NO;
}
I known why your view controller is stacks. set 'NO' flag in dimsBackgroundDuringPresentation
self.searchController.dimsBackgroundDuringPresentation = NO;
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 ran into a problem where the UISearchController, when it animates the searchBar into the navigationBar, leaves the statusBar transparent and not the same color as the searchBar, like below:
I searched SO, and the below solution inspired by this question
- (void)willPresentSearchController:(UISearchController *)searchController {
self.navigationController.navigationBar.translucent = YES;
}
- (void)willDismissSearchController:(UISearchController *)searchController {
self.navigationController.navigationBar.translucent = NO;
}
fixes the problem for iOS9, but not for iOS8. I tried setting
self.extendedLayoutIncludesOpaqueBars = YES;
but with no effect. Also, setting
self.edgesForExtendedLayout = UIRectEdgeAll;
hides the initial searchBar under the navigationBar, so I cannot use it.
Here's my code for initiating a UISearchController under an opaque UINavigationBar:
UITableViewController *searchResultsController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
searchResultsController.tableView.dataSource = self;
searchResultsController.tableView.delegate = self;
self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
self.searchController.delegate = self;
self.searchController.searchResultsUpdater = self;
self.searchController.searchBar.delegate = self;
self.searchController.searchBar.barTintColor = [UIColor whiteColor];
self.searchController.searchBar.frame = CGRectMake(0.0, 0.0, self.view.frame.size.width, 44.0); // Required for iOS8, not iOS9 though
[self.view addSubview:self.searchController.searchBar];
Any suggestions how to fix this for iOS8?
I have a UITableView as a UIView subview. This table view has a UISearchBar, which is working correctly, but it's changing it's position when it becomes first responder:
Code:
self.searchBar = [UISearchBar new];
self.searchBar.delegate = self;
self.searchBar.placeholder = #"Buscar";
self.searchBar.text = #"";
self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
self.searchController.delegate = self;
self.searchController.searchResultsDataSource = self;
self.searchController.searchResultsDelegate = self;
self.tableView.tableHeaderView = self.searchBar;
Is there anyway to avoid this change? I'd like it to remain at the same place when selected.
Thanks!
Here's the solution: http://petersteinberger.com/blog/2013/fixing-uisearchdisplaycontroller-on-ios-7/
UISearchDisplayController is pretty bad on iOS 7.
I have created a UITableViewController and added a UISearchDisplayController programmatically. There's no storyboard or nib file as I am doing everything programmatically. Everything seems to work perfectly except when I click on the search bar, it animates weirdly and there's some offset left to the bottom of the search bar.
Here's a video of the behaviour.
And here's the code of Table VC:
// Initialize Search Bar
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
self.searchBar.delegate = self;
self.tableView.tableHeaderView = self.searchBar;
self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
self.searchController.searchResultsDataSource = self;
self.searchController.searchResultsDelegate = self;
self.searchController.delegate = self;
// Hide the search bar
[self.tableView setContentOffset:CGPointMake(0, 44)];
I also tried [self setEdgesForExtendedLayout:UIRectEdgeNone]; on the UITableViewController's implementation but it created another issue, although it seems to fix the animation by some extent leaving some gap at the top and the same gap at the bottom of search bar.
Edit: One more thing to noteāit can be seen in the video that the result table view is properly aligned with the search bar. So is there any problem with the implementation of main table view? They are same but the former is aligned properly so I am guessing there's a problem with the table view. What do you think?
iOS7 makes programmatically adding these elements unnecessarily difficult. After much testing in my own app, here's how I did it inside of my UITableViewController.
// _searchBar
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.view.frame.size.width, 44.0f)];
self.searchBar.delegate = self;
// _searchController
self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:[self searchBar] contentsController:self];
self.searchController.delegate = self;
self.searchController.searchResultsDelegate = self;
self.searchController.searchResultsDataSource = self;
self.searchController.searchResultsTableView.frame = [self.tableView frame];
// _tableView
self.tableView.tableHeaderView = [self.searchController searchBar];
Don't worry about hiding the searchBar, as the OS will reset everything to its original state when you hit cancel.