iOS7: Not able to add search bar to Navigation Controller - ios

In my SearchViewController.m, I added
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
self.searchDisplayController.displaysSearchBarInNavigationBar = YES;
[self.searchDisplayController.searchBar setBarTintColor:[UIColor whiteColor]];
self.searchResultsView.dataSource = self;
self.searchResultsView.delegate = self;
NSLog(#"fetching businesses");
[self fetchBusinesses];
// fix UITableViewCell height
self.tableView.rowHeight = 90;
}
But I can not see search bar. All I see is
What am I missing?
Thanks

You can add search bar programatically like follows:
make property of both UISearchBar and UISearchDisplayController i.e.
#property (nonatomic,strong) UISearchBar *searchBar ;
#property (nonatomic,strong) UISearchDisplayController *searchDisplayController;
then write following code:
- (void)viewDidLoad
{
searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
searchBar.delegate =self;
self.searchBar.showsCancelButton =TRUE;
[self.searchBar setHidden:NO];
[self.searchBar becomeFirstResponder];
[self.searchDisplayController setActive:YES];
[self.view addSubview:self.searchBar];
self.searchDisplayController =[[UISearchDisplayController alloc]initWithSearchBar:self.searchBar contentsController:self];
self.searchDisplayController.delegate =self;
self.searchDisplayController.searchResultsDataSource= self;
self.searchDisplayController.searchResultsDelegate = self;
}
also you need to implements UISearchDisplayDelegate, UISearchBarDelegate delegates for the same ViewController
Hope This code will work for you . Give it a try and let me know your feedback.

Now i find the answer , i just tested it on my side and its working fine at my end ,
// init search bar
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
searchBar.delegate = self;
searchBar.showsCancelButton=YES;
// set up searchDisplayController
UISearchDisplayController *searchController = [[UISearchDisplayController alloc]
initWithSearchBar:searchBar contentsController:self];
searchController.delegate = self;
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
self.searchDisplayController.displaysSearchBarInNavigationBar = YES;
[self.searchDisplayController.searchBar setBarTintColor:[UIColor whiteColor]];

Related

How to dismiss a UISearchBar programmatically?

I'm using a UISearchController and a UITableView. When a cell in the UITableView is selected, I'm adding a black UIView above the whole screen with some subviews on it. It works just fine when the UISearchController is inactive. But when it's active, the UISearchBar shows above my black view.
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.scopeButtonTitles = [NSArray array];
self.searchController.searchBar.delegate = self;
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.searchController.searchBar.backgroundImage = [self imageWithColor:[MySingleton shared].barTintColor];
[self.searchController.searchBar sizeToFit];
[self.view addSubview:self.searchController.searchBar];
myView = [[UIView alloc] initWithFrame:CGRectMake(0, -64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
myView.backgroundColor = [UIColor blackColor];
myView.layer.opacity = 0.9;
[self.view addSubview:myView];
I've tried make it inactive before placing the view,
self.searchController.active = NO;
and add myView to a navigation controller's view
[self.navigationController.view addSubview:myView];
and still no luck. Any idea?
There is a method that will allow you to insert a view above another view in the view hierarchy called: - insertSubview:aboveSubview:. Here's how you could use it:
if (self.searchController.searchBar)
[self.view insertSubview:myView aboveSubview:self.searchController.searchBar];
else
[self.view addSubview:myView];
Try this
[self.searchController setActive:NO];
This is a property of SearchController as per Apple documentation, should do the trick

Adding UISegmentedControl in UISearchDisplayController

I want to add the UISegmentedControl below the searchBar and above the TableView of UISearchDisplayController. Currently UISearchDisplayController only shows its tableView under its SearchBar.
But i want to add a UISegmentedControl below the SearchBar so that I have SearchBar on the top, after SearchBar I have a UISegmentedControl and below that UISegmentedControl I have UITableView. Is that any way to do this using UISearchDisplayController or i have to make my own SearchDisplayController that have its own SearchBar , UISegmentedControl and TableView?
any suggestion? Thanks
Enable ShowScopeBar and add as many as segments by adding scop titles and handle them by following method
By Code
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[searchBar setShowsScopeBar:YES];
[searchBar setScopeButtonTitles:#[#"button1",#"button2"]];
[[self view] addSubview:searchBar];
By XIB
Delegate method to handle scope button action
- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope{
}
Create a view in table
UIView *viewsearch=[[UIView alloc]initWithFrame:CGRectMake(0,-10, 320,83)];
[self.tblname addSubview:viewsearch];
[self.view addGestureRecognizer:revealController.panGestureRecognizer]
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,5, 320, 40)];
searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
searchBar.delegate=self;
[searchBar setBarTintColor:[UIColor colorWithRed:(249/255.0) green:(9/255.0) blue:(99/255.0) alpha:1]];
searchBar.tintColor = [UIColor whiteColor];
searchBar.placeholder = #"Search items e.g. jacket";
Addsearchbar view in that view.
[viewsearch addSubview:searchBar];
searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
Use UISegmentedControl in this searchview.
NSArray *itemArray = [NSArray arrayWithObjects: #"General", #"Near me", nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
segmentedControl.frame = CGRectMake(50,53, 225, 22);
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
[segmentedControl addTarget:self action:#selector(MySegmentControlAction:) forControlEvents: UIControlEventValueChanged];
segmentedControl.selectedSegmentIndex = 0;
segmentedControl.tintColor = [UIColor colorWithRed:(249/255.0) green:(10/255.0) blue:(99/255.0) alpha:1];
segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleBottomMargin;
[viewsearch addSubview:segmentedControl];
I find the solution for my Problem that i added the UISegmentedControl as a Header of TableView of UISearchDisplayController. In this way it just looked like that i have added a UISegmentedControl Separately under the searchBar of UISearchDisplayController.
Thank You every one who tried to help.

stange behaviour UIRefreshControl with UISearchBar

i'm using UIRefreshControl with UISearchBar when i'm pull to refresh i see a white background at the top of uiviewcontroller
UISearchBar *searchBar =
[[UISearchBar alloc] initWithFrame:
CGRectMake(0, 0, self.tableView.frame.size.width, 44.0)];
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
self.tableView.tableHeaderView = searchBar;
_refreshControl = [[UIRefreshControl alloc] init];
[_refreshControl addTarget:self action:#selector(update) forControlEvents:UIControlEventValueChanged];
[self.tableView addSubview:_refreshControl];
the problem is the white background above
any ideas?
Create a background view for the table and set its color to clearColor, like this
- (void)viewDidLoad {
self.tableView.backgroundView = [UIView new];
self.tableView.backgroundView.backgroundColor = [UIColor clearColor];
}

UISearchBar display issue

The searchbar's right edge is wider than the left, I don't know why? This is not cool.Please help me with this.Thank you very much.
Here's my code:
#property (nonatomic, strong) UITableView *tableView;
#property (nonatomic, strong) UISearchBar *searchBar;
#property (nonatomic, strong) UISearchDisplayController *strongSearchDisplayController;
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
self.searchBar.backgroundColor = [UIColor clearColor];
self.searchBar.placeholder = #"搜索";
self.searchBar.delegate = self;
[self.searchBar sizeToFit];
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0, 0.0, SCREENWIDTH, SCREENHEIGHT - NAVIGATIONBARHEIGHT - TABBARHEIGHT ) style:UITableViewStylePlain];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.view addSubview:self.tableView];
self.tableView.tableHeaderView = self.searchBar;
self.strongSearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
self.searchDisplayController.searchResultsDataSource = self;
self.searchDisplayController.searchResultsDelegate = self;
self.searchDisplayController.delegate = self;
self.searchDisplayController.searchResultsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
My advice would be to implement your search bars frame this way:
CGRectZero is the equivalent to (0,0,0,0)
Additionally it's best to add the search bar to a view. Like so:
- (void) viewDidLoad:(BOOL)animated {
UIView *searchbarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)]; //This adds a container that will hold the search bar.
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
self.searchBar.delegate = self;
[searchbarView addSubview:self.searchBar];
self.tableView.tableHeaderView = searchbarView; //Inserts UIView into the header of self.tableView
}
You can make following:
Add top offset to your table that is equal to searchBar height:
self.tableView.contentInset = UIEdgeInsetsMake(searchController.searchBar.frame.height, 0, 0, 0);
Add your searchBar to the tableView directly, not to the tableHeaderView:
// remove this
self.tableView.tableHeaderView = self.searchBar;
// insert this
[self.tableView addSubview:self.searchBar];
Please be sure that searchBar rect has correct origin (0, 0)
Note: It will fix your problem, but searchBar's behaviour will be a bit different from original: search bar will be visible by default, there will be no auto-hide of search bar when it's half-closed, etc.

Transforming UISearchbar

I have a search-bar with tableView below that search bar. I want to go full screen when the user tap search-bar for searching. But i am unable to set the frame to the origin of the UISearchBar.
i doing it in this way.
-(void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
[ [UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
[self.topBarView setHidden:YES];
[UIView animateWithDuration:1.0
delay:0.0
options:0
animations:^{
[self.searchBar setFrame:CGRectMake(0.0,0.0, self.searchBar.frame.size.width,
self.searchBar.frame.size.height )];
}
completion:^(BOOL completion){
}];
[self.searchBar setNeedsDisplay];
}
}
I am hiding all the views above the search-bar and i am setting the origin to 0,0
In the second image the top bar disappears but search-bar stays on its place. It should go at the top.
If somebody can identify the problem it will be great.
I am also using auto-layout does this make cause any problem?
use searchbar display controller.it will achieve that
Try this.
Add a search Bar & searchDisplay Controller to your controller.
UISearchBar *searchBar;
UITableView *tableView;
UISearchDisplayController *searchController;
// Create table view
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.backgroundColor = [UIColor whiteColor];
_tableView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
[self.view addSubview:_tableView];
// Create search bar
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.view.bounds.size.width, 44.0f)];
_searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
_searchBar.delegate = self;
[self.view addSubview:_searchBar];
[_searchBar sizeToFit];
// Create search controller
_searchController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
_searchController.searchResultsDataSource = self;
_searchController.searchResultsDelegate = self;
_searchController.delegate = self;
Then you can perform this operation so that searchBar becomes responder & shows Keyboard.
[_searchBar becomeFirstResponder];
Hope that helps.

Resources