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];
Related
I've making practises to use iOS 8 new features - UISearchController to display my tableView and result. But something strange happened. It seems like the searchBar is transparent.
Yes, the searchBar is overlapping with the tableView. I've search a lot in SO, but no help.
My implementation in viewDidLoad
self.myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
_myTableView.delegate = self;
_myTableView.dataSource = self;
[self.view addSubview:_myTableView];
self.mySearchController = [[UISearchController alloc] initWithSearchResultsController:nil];
_mySearchController.searchResultsUpdater = self;
_mySearchController.dimsBackgroundDuringPresentation = NO;
_mySearchController.hidesBottomBarWhenPushed = YES;
_mySearchController.hidesNavigationBarDuringPresentation = YES;
_mySearchController.searchBar.searchBarStyle = UISearchBarStyleMinimal;
[_mySearchController.searchBar sizeToFit];
self.myTableView.tableHeaderView = self.mySearchController.searchBar;
Did I miss something important?
It's because you've set _mySearchController.searchBar.searchBarStyle = UISearchBarStyleMinimal. According to Apple's documentation:
UISearchBarStyleMinimal - The search bar has no background, and the search field is translucent.
Try deleting that line of code or setting it to UISearchBarStyleDefault instead.
In my app I'm trying to add search functionality. I have table view controller and custom top bar view where I show UISearchBar. The problem is that overlay view is always a bit under the top bar and it adds gap between them:
In my table view controller .m file's viewDidLoad:
[super viewDidLoad];
LSDropdownViewController *menuCtrl = (LSDropdownViewController *)[self parentViewController];
menuCtrl.topSearchBar.delegate = self;
[menuCtrl.topSearchBar setBackgroundColor:[UIColor clearColor]];
[menuCtrl.topSearchBar setBackgroundImage:[UIImage imageWithGradientColors]];
[menuCtrl.topSearchBar setTintColor:[UIColor whiteColor]];
self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:menuCtrl.topSearchBar contentsController:self];
self.searchController.delegate = self;
self.searchController.searchResultsDataSource = self;
self.searchController.searchResultsDelegate = self;
any ideas?
It's due to iOS7+. So, to get rid of it, you have to set search bar frame manually by offsetting y-value 20 pixels i.e. searchBar.frame = CGRectMake(0,20,width,height).
I embeded UISearchBar in navigationBar in my viewcontroller. Here is the code .I load SearchBar and UISearchDisplayController programmatically.
- (void)viewDidLoad
{
[super viewDidLoad];
UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)];
searchBar.tintColor = [UIColor darkGrayColor];
searchBar.showsSearchResultsButton = NO;
self.searchController = [[UISearchDisplayController alloc]initWithSearchBar:searchBar contentsController:self];
self.searchController.delegate = self;
searchBar.delegate = self;
[self.navigationItem setTitleView:searchBar];
}
And in viewWillAppear ,I add background image to my navigationBar.That's it. Not a complex view. It is working fine in iOS7. When user tap on SearchBar,it changes to editting mode searchBar is in the same position (x,y) . But when I tested on iOS6 , when tap on searchBar , we enters the editting mode but searchBar shifting up to above the screen. It is a behavior of searchDisplayController that try to cover view.
How can I solve this ? I tried to search through SO but has no luck so far.
Thanks in advance
I am adding a UISearchBar to a UINavigation Bar as so (this is a storyboard based app) :
//Setup the search bar.
searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 50.0f, 44.0f)];
self.tabBarController.navigationItem.titleView = searchBar;
searchBar.delegate = self;
controller = [[UISearchDisplayController alloc]initWithSearchBar:self.searchBar contentsController:self];
controller.searchResultsDataSource = self;
controller.searchResultsDelegate = self;
No matter what I set the width and height and position of the search bar to be, it will not change the height / width of the search bar. It is always the same width and height.
Can anyone suggest how to alter ?
I've experienced the same problem one time, and what I did was adding the searchBar into a container View (i've had problems showing the cancel button) and add the container view as a left bar item
- (UIBarButtonItem *)createSearchBarHolder {
UIView *searchBarContainer = [[UIView alloc] initWithFrame:navigationSearchBar.frame];
searchBarContainer.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[searchBarContainer addSubview:navigationSearchBar];
navigationSearchBar.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
UIBarButtonItem *searchBarHolder = [[UIBarButtonItem alloc] initWithCustomView:searchBarContainer];
return searchBarHolder;
}
And in viewDidLoad:
NSArray *leftBarButtons = [[NSArray alloc] initWithObjects:[self createSearchBarHolder], nil];
self.navigationItem.leftBarButtonItems = leftBarButtons;
It's not clear what size you want your search bar to be. If you make it the navigation item's titleView, it will adjust for any buttons on the navigation bar, and adjust on rotation. Is that what you want?
- (void)viewDidLoad {
[super viewDidLoad];
UISearchBar *sb = [[UISearchBar alloc] init];
self.navigationItem.titleView = sb;
}
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];