In some device types mostly in 6s, the UISearchController doesn't span fully.
//Init search controller
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
UISearchBar *searchBar = self.searchController.searchBar;
searchBar.frame = self.searchBarHolder.bounds;
self.searchController.searchResultsUpdater = self;
self.searchController.delegate= self;
self.searchController.searchBar.delegate=self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.hidesNavigationBarDuringPresentation = NO;
[self.searchBarHolder addSubview:searchBar];
[self.searchBarHolder layoutIfNeeded];
[searchBar sizeToFit];
where searchBarHolder is set in storyboard with constraints trailing-0, top-0, leading-0, height - 45
This produces the following result :
and once the search bar is clicked it will becomeFirstResponder and there after, it displays properly.
I have no clue why it displays like that at first place.
This UIViewController is embedded in UINavigationCOntroller, and this is embedded in UITabBarController as one of the tab item.
Replace this Line
searchBar.frame = self.searchBarHolder.bounds;
With this
CGRect rect = self.searchBarHolder.bounds;
rect.size.width = [UIScreen mainScreen].bounds.size.width;
searchBar.frame = rect
Related
I have similar problem like here:
SearchBar in NavigationBar IOS 11
In my case I don't have toolbar on top of TableView.
I added searchBar in my VC:
- (void)viewDidLoad {
[super viewDidLoad];
UISearchController * search = [[UISearchController alloc] initWithSearchResultsController:nil];
search.searchResultsUpdater = self;
self.navigationItem.searchController = search;
self.navigationItem.hidesSearchBarWhenScrolling = YES;
self.navigationController.navigationBar.prefersLargeTitles = NO;
self.navigationController.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeNever;
}
Now searchBar is always visible. It should be hide by default. SearchBar should showed when I pull the tableView down.
What am I doing wrong?
The problem is that I add picture to background:
UIImageView *background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"somethingImage"]];
[background setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:background];
[self.view sendSubviewToBack:background];
Then Search controller has some problem. I don't know how to repair it. I think it is an apple bug.
So I've got a UITableView with a UISearchController. It loads up fine, then I do my search and select a result, pushing a new view controller to my UINavigationController.
If, while the new VC is onscreen, I rotate my phone, then rotate back, and go back...when I get back to the UITableView, the UISearchBar is nowhere to be seen. If, to troubleshoot, I hide the UINavigationBar, or if I look at the view hierarchy, I find that it has moved up behind the navigation bar and is being covered.
I've tried all kinds of hackey solutions, like moving the frame of the bar after it finishes loading, with no success. I really need help on this one, I'm stumped. None of the stuff I've found on google has helped.
Here are screenshots of before, after, and the view heirarchy:
Screenshots
Here is all the View-related code:
- (void)viewDidLoad {
[super viewDidLoad];
if (!self.title)
self.title = #"GLOBAL SEARCH";
self.tableView.backgroundColor = [UIColor whiteColor];
//TESTING
//if I don't have this, the bar detaches from the navigation bar
self.edgesForExtendedLayout = UIRectEdgeAll;
self.extendedLayoutIncludesOpaqueBars = YES;
//test
self.definesPresentationContext = YES;
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.dimsBackgroundDuringPresentation = NO;
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.searchController.searchBar.backgroundImage = [[UIImage alloc] init];
self.searchController.searchBar.backgroundColor = kConstantBaseColor;
self.searchController.searchBar.barTintColor = kConstantBaseColor;
self.searchController.searchBar.tintColor = kConstantLabelColor;
//set color
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes:#{NSForegroundColorAttributeName:kConstantLabelColor}];
self.searchController.hidesNavigationBarDuringPresentation = NO;
[self.searchController.searchBar setSearchBarStyle:UISearchBarStyleMinimal];
self.tableView.tableHeaderView = self.searchController.searchBar;
self.tableView.clipsToBounds = NO;
self.tableView.tableHeaderView.clipsToBounds = NO;
self.tableView.tableHeaderView.backgroundColor = kConstantBaseColor;
//put purple block above, in case user scrolls up
CGRect bufferFrame = CGRectMake(self.searchController.searchBar.frame.origin.x,
self.searchController.searchBar.frame.origin.y-300,
self.searchController.searchBar.frame.size.width*2,
300);
UIView *purpleBuffer = [[UIView alloc] initWithFrame:bufferFrame];
purpleBuffer.backgroundColor = kConstantBaseColor;
[self.tableView addSubview:purpleBuffer];
}
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
[coordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
}];
}
Does anyone have any solutions? I'm stumped and have been beating my head against it for days. Thank you so much.
I build the searchController and the tableview.
But I refer more web documents. There are always add the searchBar to tableview header.
like below:
self.displayTableView.tableHeaderView = self.searchController.searchBar;
But Now I don't want to add the searchBar to the tableHeaderView.
I try to add direct the view(searchBarBgVW ). But the searchrBar was not show at screen.
as below:
- (void)viewDidLoad {
[super viewDidLoad];
......
.........
// --- search bar declare ---
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
// 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.displayTableView.tableHeaderView = self.searchController.searchBar;
self.searchController.searchResultsUpdater = self;
self.searchController.searchBar.frame = CGRectMake(0, 0, self.searchController.searchBar.frame.size.width, 44.0);
[self.searchBarBgVW addSubview: self.searchController.searchBar];
self.searchController.searchBar.delegate = self;
self.definesPresentationContext = YES;
.....
....
..
}
If I open the comment about the tableheader like below:
self.searchController.searchBar.frame = CGRectMake(0, 0, self.searchController.searchBar.frame.size.width, 44.0);
self.searchController.searchResultsUpdater = self;
self.displayTableView.tableHeaderView = self.searchController.searchBar;
[self.view addSubview: self.searchController.searchBar];
The search bar will show at the place (0,0),
But the TableHeader will show white block.
How can I add the searchbar place using the searchController at correct method?Or How can I remove the tableviewheader white block?
Thank you very much.
Apple has a (newish) UICatalog sample that includes all kinds of search capabilities. It is divided into pretty simple controllers.. It's provided in both Swift and ObjC.
I'm attempting to position my UISearchBar directly under my UINavigationBar but I'm having issues where it's not appearing at all. I'm using iOS8's new searchController.
self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
self.searchController.searchResultsUpdater = 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.salesTableView.tableHeaderView = self.searchController.searchBar;
Currently I'm using that which correctly adds it to my UITableView header, but the issue with that is that it scrolls with the table which is not what I want. I want it above my tableview so it sticks instead and tried this but it seems to be gone now, unless I'm not using the right
values?
self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
self.searchController.searchResultsUpdater = self;
self.searchController.searchBar.frame = CGRectMake(0, 0, self.searchController.searchBar.frame.size.width, 44.0);
// self.salesTableView.tableHeaderView = self.searchController.searchBar;
Because the origin is (0,0), it will be hidden by status bar and navigation bar. You should see the search bar if changing to:
self.searchController.searchBar.frame = CGRectMake(0, 64, self.searchController.searchBar.frame.size.width, 44.0);
I have two solution. I will show you the easy way (you could also use storyboard): Create a UIView in your ViewController, named searchBarView; Add self.searchController.searchBar into the searchBarView; Add searchBarView to your ViewController.
UIView *searchBarView = CGRectMake(0, 64, self.searchController.searchBar.frame.size.width, 44);
[searchBarView addSubView:self.searchController.searchBar];
[self.view addSubView:searchBarView];
I assume that you setup search controller self.searchController.searchBar.sizeToFit() in viewDidload. If it is the universal app, you need to add this method (at least in my case to make everything work):
- (void)viewDidLayoutSubviews {
// you could check
// if (IS_IPAD || (IS_IPHONE_6PLUS && UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)))
[self.searchController.searchBar sizeToFit];
}
This is my first answer on stackoverflow. Hope this help ^^.
Two things to check out:
Try setting automaticallyAdjustsScrollViewInsets to false on your UIViewController. That should shift the subviews of your view controller's view to below the navigation bar.
Use searchFieldBackgroundPositionAdjustment (available iOS 5+) to adjust the position of the search field inside of your UISearchBar
Usage:
let searchBar = UISearchBar()
searchBar.searchFieldBackgroundPositionAdjustment = UIOffset(horizontal: 0, vertical: 10)
What I want to achieve is a UISearchBar that moves up and covers the UINavBar, and contains a cancel button on the right of it. All goes well, until I use the following line of code:
searchDC = [[[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self] autorelease];
What ends up happening is the UITableView just won't scroll, but everything else functions as expected. If I remove that line, my nav bar is visible, and the search bar just sits below it, also lacking a cancel button.
Any ideas?
The code for drawing the search bar is:
self.navigationItem.title = #"Search";
searchBar = [[[UISearchBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.view.bounds.size.width, 44.0f)] autorelease];
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
searchBar.keyboardType = UIKeyboardTypeAlphabet;
searchBar.delegate = self;
[searchBar becomeFirstResponder];
tableView.tableHeaderView = searchBar;
tableView.tableHeaderView.frame = CGRectMake(0.f, 0.f, 480.f, 44.f);
searchDC = [[[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self] autorelease];
searchDC.searchResultsDataSource = self;
searchDC.searchResultsDelegate = self;
searchDC.searchResultsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
searchDC.searchResultsTableView.scrollEnabled = YES;
overlayView.frame = CGRectMake(0, 50, 320, 480);
[self.view addSubview:overlayView];
[self.view bringSubviewToFront:overlayView];
Not enough information to answer this. You need to show the UIViewController or UINavigation Controller code (both the .h and .m) where you are setting up the UISearchDisplayController.
EDIT:
You're implementing this totally wrongly. Apple has a great example on how to implement this http://developer.apple.com/iphone/library/samplecode/TableSearch/Introduction/Intro.html
From Apple's Documentation,
Delegate for the search display controller (delegate), which responds to events such the starting or ending of a search, and the showing or hiding of the search interface.
Set the delegate to your UITableViewController
searchDC.delegate = self;
Also add the searchDC's searchBar to the tableView's headerView
[self.tableView setTableHeaderView:searchDC.searchBar];