With the following code the scope bar is invisible (buttons work but are not visible - just black space)
UISearchBar* searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 44 + (_showScope?40:0));];
searchBar.barStyle = UIBarStyleBlack;
searchBar.scopeButtonTitles = [NSArray arrayWithObjects:#"string1", #"string2", nil];
searchBar.showsScopeBar = YES;
searchBar.delegate = self;
self.tableView.tableHeaderView = searchBar;
works fine in iOS6. Any ideas?
this seems to be a workaround:
UISearchBar* searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
searchBar.barStyle = UIBarStyleBlack;
searchBar.scopeButtonTitles = [NSArray arrayWithObjects:#"string1", #"string2", nil];
searchBar.showsScopeBar = YES;
searchBar.delegate = self;
self.tableView.tableHeaderView = searchBar;
searchBar.frame = CGRectMake(0, 0, self.tableView.frame.size.width, 44 + (_showScope?40:0));
nice!
Thank you to Nick H247 for getting me on the right track. However, his workaround produced another issue for me where the search result list was being displayed behind the search bar, making it so I was unable to select search results from the beginning of the list. I fixed this by setting the frame before assigning the UISearchBar to the tableHeaderView.
UISearchBar* searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
searchBar.barStyle = UIBarStyleBlack;
searchBar.scopeButtonTitles = [NSArray arrayWithObjects:#"string1", #"string2", nil];
searchBar.showsScopeBar = YES;
searchBar.delegate = self;
searchBar.frame = CGRectMake(0, 0, self.tableView.frame.size.width, 44 + (_showScope?40:0));
self.tableView.tableHeaderView = searchBar;
Simple, calls searchBar.sizeToFit;
Related
I used UISearchBar display on UINavigationBar by setting titleView of UINavigationBar at ViewDidLoad.
UISearchBar *searchBar = [[UISearchBar alloc] init];
searchBar.searchBarStyle = UISearchBarStyleMinimal;
searchBar.tintColor = [UIColor blackColor];
searchBar.delegate = self;
searchBar.showsCancelButton = NO;
searchBar.backgroundImage = [[UIImage alloc] init];
self.navigationItem.titleView = self.searchBar;
It works, but I had a problem. When I push into NavigationController, UISearchBar delay display until view did appear. I think it seem have to animated when set into titleView. What was problem?
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.
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]];
I can not search because my search bar is a header of tableView and then I reload tableView I have not search results.
I know there is a lot of ways to solve it but what is more wisely?
My code
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)sectionIndex
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 10, 270, kRowHeight)];
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 270, kRowHeight)];
self.searchBar.delegate = self;
view.opaque = NO;
view.backgroundColor = [UIColor clearColor];
self.searchBar.opaque = NO;
self.searchBar.translucent = NO;
self.searchBar.backgroundColor = [UIColor clearColor];
[view addSubview: self.searchBar];
self.searchBar.barStyle = UISearchBarStyleDefault;
self.searchBar.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin ;
self.searchBar.barTintColor = [UIColor clearColor];
return view;
}
And I want that then I scroll tableView my searchBar will disappear in top and then I scroll up it will appear. Is any simple to do it?
You need to use tableHeaderView and not section header. Just put in your viewDidLoad
UISearchBar* searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 270, kRowHeight)];
self.tableView.tableHeaderView = searchBar;
I want my search bar to look like this:
this is how i created the search bar:
search = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 308, 27)];
search.delegate = self;
search.center = CGPointMake(self.frame.size.width/2, 10 +search.frameHeight/2);
search.placeholder = NSLocalizedString(#"search foods you like or dislike", nil);
search.backgroundImage = nil;
search.backgroundColor = [UIColor clearColor];
but this is what i get :
what am i doing wrong ? how do i get rid of the extra grey edges on the sides of the textField ?
You can achieve that with the following code:
search.barTintColor = [UIColor clearColor];