I got a problem with the uisearchdisplaycontroller.
If I add the controller's searchbar to a tableviews headerview it shows a thin white line above the searchbar! If I add the searchbar without the searchcontroller it shows it correctly. Important is that u take a really close look to the standard searchdisplaycontroller or customize it a bit, otherwise you will not be able to see the line.
Does anyone knows a way to avoid the white line?
Code in init methode of BaseViewController : UIViewController
searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
[searchBar sizeToFit];
searchBar.showsCancelButton = NO;
searchBar.barStyle = UIBarStyleDefault;
searchBar.hidden = NO;
searchBar.delegate = self;
searchBar.placeholder = #"Suche";
searchCtrl = [[UISearchDisplayController alloc]
initWithSearchBar:searchBar contentsController:self];
searchCtrl.delegate = self;
searchCtrl.searchResultsDataSource = self;
searchCtrl.searchResultsDelegate = self;
[searchCtrl searchResultsTableView].scrollsToTop = NO;
Code in StartViewController : BaseViewController in viewWillAppear
tableController.tableView.tableHeaderView = searchCtrl.searchBar;
searchBar.hidden = NO;
if ( [tableController.tableView contentOffset].y == 0.0 )
{
[tableController.tableView setContentOffset:CGPointMake(0.0, 44.0) animated:NO];
}
Nothing special, for me it has to do with the UISearchDisplayController not with the implementation, cause if u only use the searchbar everything is fine
set searchBar.clipsToBounds = YES;
Related
i'm recently find this awesome modal view popover (source).
"Usage" section there is telling how to show RNBlurModalView with UIView in it with following code:
RNBlurModalView *modal = [[RNBlurModalView alloc] initWithViewController:self view:view];
[modal show];
Everything works fine when i'm using RNBlurModalView to show custom UIView like square with following code:
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 280, 200)];
view.backgroundColor = [UIColor redColor];
view.layer.cornerRadius = 5.f;
view.layer.borderColor = [UIColor blackColor].CGColor;
view.layer.borderWidth = 5.f;
RNBlurModalView *modal = [[RNBlurModalView alloc] initWithViewController:self view:view];
[modal show];
, but i'm having problem when i'm trying to show UIView from UIViewController in RNBlurModalView, it just shows empty modal view, here's code that i'm using:
ExampleViewContoller *exampleVC = [[ExampleViewContoller alloc]init];
RNBlurModalView *modal = [[RNBlurModalView alloc] initWithViewController:self view:exampleVC.view];
[modal show];
Here is image of ExampleViewController from storyboard if needed -
Question : any ideas what am i doing wrong, why modal view is empty?
Well the problem is with which you are calling for viewcontrollers view .
Try this
UIStoryboard *storyBoard=[UIStoryboard storyboardWithName:#"MainStoryboard" bundle:[NSBundle mainBundle]];
ExampleViewContoller *exampleVC = [storyBoard instantiateViewControllerWithIdentifier:#"ExampleViewContoller"];;
RNBlurModalView *modal = [[RNBlurModalView alloc] initWithViewController:self view:exampleVC.view];
[modal show];
Note dont forget to set the storyboardId of the ExampleViewContoller as "ExampleViewContoller"in the storyboard.
I set up my searchDisplayController and searchBar like so:
UISearchBar *aSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 150, 44)];
self.reportSearchBar = aSearchBar;
_reportSearchBar.tintColor = DARKGRAY_COLOR;
_reportSearchBar.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
_reportSearchBar.delegate = self;
[aSearchBar release];
UISearchDisplayController *searchDisplayCtlr = [[UISearchDisplayController alloc] initWithSearchBar:self.reportSearchBar contentsController:self];
self.reportSearchDisplayController = searchDisplayCtlr;
self.reportSearchDisplayController.searchResultsDataSource = self;
self.reportSearchDisplayController.searchResultsDelegate = self;
[searchDisplayCtlr release];
UIBarButtonItem *searchBBI = [[UIBarButtonItem alloc] initWithCustomView:_reportSearchBar];
self.reportSearchBBI = searchBBI;
[searchBBI release];
[self.navigationItem setRightBarButtonItem:_reportSearchBBI animated:NO];
I checked if my ViewController conforms to the class just in case:
if ([self conformsToProtocol:#protocol(UISearchDisplayDelegate)]) {
NSLog(#"conform to search display");
}
My ViewController .h file has:
<UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate, UISearchDisplayDelegate>
however, I set a break point at
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
[self filterContentForSearchText:searchString];
return YES;
}
And it never reaches there. I implement one of the UISearBarDelegate methods and it does get to that method. When I run Apple's example code TableSearch, the searchDisplayController delegate method copied above does get run. So for me, I try to put in text into the search bar and my app crashes since the filtered list has no objects in it since the searchDisplayController delegate never gets called.
Thoughts? Thanks!
I just see in the apple reference:
searchController = [[UISearchDisplayController alloc]
initWithSearchBar:searchBar contentsController:self];
searchController.delegate = self;
searchController.searchResultsDataSource = self;
searchController.searchResultsDelegate = self;
I dont see the searchController.delegate = self in your code, isn't that nessesary?
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UISearchDisplayController_Class/Reference/Reference.html
Make sure you create an iVar for the UISearchDisplayController in your header file.
If you create an UISearchDisplayController using:
UISearchDisplayController* searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchField contentsController:self];
it will get released by ARC, it will not call any delegate methods and when you'll call self.searchDisplayController (the UIViewController's property) it will be nil.
So, the fix is:
In your header (.h) file:
#interface MenuViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchDisplayDelegate> {
UISearchDisplayController* searchDisplayController;
UISearchBar *searchField;
UITableView* tableView;
NSArray* searchResults;
}
and in the implementation (.m) file:
searchField = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 49)];
searchField.delegate = self;
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchField contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
searchDisplayController.searchResultsDelegate = self;
tableView.tableHeaderView = searchField;
tableView.contentOffset = CGPointMake(0, searchField.frame.size.height);
When implemented like that, you can call both self.searchDisplayController and searchDisplayController in the rest of your code.
In order to understand how UISearchDisplayController works i've written the following code in viewDidLoad method:
UISearchBar * searchBar = [[[UISearchBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 40.0f)] autorelease];
searchBar.delegate = self;
searchBar.showsCancelButton = YES;
_searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
_searchController.delegate = self;
_searchController.searchResultsDataSource = self;
_searchController.searchResultsDelegate = self;
[_searchController setActive:YES animated:YES];
[_searchController.searchBar becomeFirstResponder];
But when i run my code the UISearchBar doesn't being shown as you can see in the image.
You create UISearchBarcontroller its good but not added in uiviewcontroller. So You add UISearchBarcontroller in uiviewcontroller.
coding:
[self.view addsubview:_searchController];
I would like to modify the tableSearch sample code, so that there is no nib for the tableview. As it is already a tableviewcontroller subclass, I can call the tableview like this:
MainViewController *mainViewController = [[MainViewController alloc] initWithStyle:UITableViewStylePlain];//NibName:#"MainView" bundle:nil];
Without the nib, I would like to add the search bar manually. I tried below:
UISearchBar* searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,10, 320, 41)];
[searchBar setFrame:CGRectMake(0,10, 320, 41)];
searchBar.delegate = self;
searchBar.tintColor = [UIColor redColor];
[searchBar sizeToFit];
self.tableView.tableHeaderView = searchBar;
The searchbar does appear ok. Now I would like the search action of this searchbar to point to the methods that I already have in my code, i.e searchDisplayController delegate methods.
Is there anyway I can point the self.searchDisplayController's Search bar property to my custom searchbar, so they do the same actions? Thanks in advance...
You have to use the initWithSearchBar:contentsViewController method of UISearchDisplayController. This way, your custom search bar will be associated with the controller. Also you have to set the contentsViewController in this method.
Hm.. this is code from one of my VC. Also set UISearchBarDelegate,UISearchDisplayDelegate
_listContainer = [[UITableView alloc] init];
_listContainer.delegate = self;
_listContainer.dataSource = self;
_listContainer.separatorStyle = UITableViewCellSelectionStyleNone;
_data = [[NSArray alloc] init];
And then
_searchBar = [[NL_CustomSearchBar alloc] initWithFrame:CGRectMake(4.0f, 0, 150, 46)];
_searchBar.delegate = self;
_listContainer.tableHeaderView = _searchBar;
I want to add a custom UIImageView to UISearchDisplayController's table view background and set table view's background color to clearColor. Tried a few different approach but couldn't find the right solution. Any idea how to approach this?
Note: I don't want to add to searchDisplayController's searchResultsTableView's view hierarchy, but rather overlay another sibling view below it)
You can set the background image in a similar way you would for your main table, only set it in the searchDisplayControllerDidBeginSearch delegate method. For instance:-
- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller {
[controller.searchResultsTableView setDelegate:self];
UIImageView *anImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"gradientBackground.png"]];
controller.searchResultsTableView.backgroundView = anImage;
[anImage release];
controller.searchResultsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
controller.searchResultsTableView.backgroundColor = [UIColor clearColor]; }
You can also do this wherever you instantiate your UISearchDisplayController. In my app I was doing this in my UITableView viewDidLoad method and was matching the styles between the two tables:
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.separatorColor = [UIColor blackColor];
self.tableView.backgroundColor = [UIColor grayColor];
self.tableView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
searchController.delegate = self;
searchController.searchResultsDataSource = self;
searchController.searchResultsDelegate = self;
searchController.searchResultsTableView.separatorColor = self.tableView.separatorColor;
searchController.searchResultsTableView.backgroundColor = self.tableView.backgroundColor;
searchController.searchResultsTableView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
}