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)
Related
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
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 trying to add a searchbar to my viewcontroller. There are many tutorials with tableviewcontrollers, but not viewcontrollers. My problem is (like multiple other questions here) when selecting the searchbar the navbar moves up and searchtableview overlaps, but searchbar does not move up.
If I try to add it programatically I can implement it to the table header with:
self.tableView.tableHeaderView = mySearchBar;
In my UI there are buttons between the searchbar and tableview. I'd like the searchBar to be set right below the navigationBar. How would I do this?
UIView *searchBar_con=[[UIView alloc]initWithFrame:your_frame];
searchBar_con.backgroundColor=[UIColor clearColor];
UISearchBar *srchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 240 , searchBar_con.frame.size.height)];
srchBar.delegate = self;
[searchBar_con addSubview:srchBar];
UISearchDisplayController *searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:srchBar contentsController:self];
self.searchDisplayController.searchResultsDelegate = self;
self.searchDisplayController.searchResultsDataSource = self;
self.searchDisplayController.delegate = self;
[self.view addSubview:searchBar_con];
Disclaimer: I'm something of an iOS n00b. I have a navigation-based app -- i.e., in my AppDelegate, I create the nav frame:
self.navigation_controller = [[UINavigationController alloc] initWithRootViewController:home_view_controller];
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
self.window.rootViewController = navigation_controller;
self.window.makeKeyAndVisible;
Inside the frame of my home view, I want a search bar, then the scrollable table view. So, I wrote this:
- viewDidLoad{
(HomeView*)home_view = [[HomeView alloc] init];
self.view = home_view
self.view.backgroundColor = [UIColor whiteColor]
(UITableView*)self.table_view = [UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
self.table_view.bounds.origin.y += 44;
self.table_view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.table_view.dataSource = self;
self.table_view.delegate = self;
[self.view addSubview:self.table_view];
search_frame = self.view.bounds;
search_frame.origin.y = 48.0;
search_frame.size.height = 48.0;
self.search_bar = [[UISearchBar alloc] initWithFrame:search_frame)];
[self.view addSubview:self.search_bar];
}
The table view displays fine, but occupies all the space below the navigation bar. I want to have the navigation bar, then immediately below it the search bar, followed by the table view. At the point of viewDidLoad, the UITableView does not yet have a non-zero bounding rect and I expect that's part of the problem. Additionally, I've specified UIViewAutoresizingFlexibleHeight in my autoresizingMask when I really want it glued against the search bar -- again, I believe this may be part of the problem.
What have I misunderstood here?
Thanks
Steve,
Try following changes:
1) Set tableview's frame using:
CGRect targetRect = CGRectMake(0, 48, self.view.frame.size.width, self.view.frame.size.heigth);
(UITableView*)self.table_view = [UITableView alloc] initWithFrame:targetRect style:UITableViewStylePlain];
2) Add following autosize mask also:
// This value will anchor your table view with top margin
UIViewAutoresizingFlexibleBottomMargin
3) Set searchbar's frame to following rect frame:
CGRecttMake(0, 0, self.view.frame.size.width, 48);
What I would do, is just add the UISearchBar as the first UITableView header.
(method: tableView:viewForHeaderInSection:)
Check this blog post:
http://jainmarket.blogspot.com/2009/08/add-searchbar-as-uitableviews-header.html
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];