I'm trying to recreate this UISearchBar (as seen in the Table Search example code):
alt text http://img168.imageshack.us/img168/6378/43558113.png
All the examples I've seen to do this involve using a xib, however I need to do it programmatically. The problem is changing the tint color also changes the cancel button's tint:
alt text http://img243.imageshack.us/img243/1375/screenshot20100527at944.png
Any ideas?
Associating the search bar with a UISearchDisplayController magically provides a lot of standard look and behavior such as:
gray tint without affecting cancel button
auto showing/hiding of cancel button
width adjustment around any tableview indexes
In my tableview controller I've done the following:
- (void)viewDidLoad {
[super viewDidLoad];
// setup searchBar and searchDisplayController
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
[searchBar sizeToFit];
searchBar.delegate = self;
searchBar.placeholder = #"Search";
self.tableView.tableHeaderView = searchBar;
UISearchDisplayController *searchDC = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
// The above assigns self.searchDisplayController, but without retaining.
// Force the read-only property to be set and retained.
[self performSelector:#selector(setSearchDisplayController:) withObject:searchDC];
searchDC.delegate = self;
searchDC.searchResultsDataSource = self;
searchDC.searchResultsDelegate = self;
[searchBar release];
[searchDC release];
}
I totally agree with Scott McCammon.
However using a performSelector:withObject: on setSearchDisplayController: would not be my approach. This depends on private API which can change at any moment. If Apple would remove their private implementation your app will crash.
A better way would be to override the searchDisplayController: in your view controller to return your instance of UISearchDisplayController:
- (UISearchDisplayControlelr *) searchDisplayController {
return yourInstanceOfASearchController;
}
I don't understand the need for the call to setSearchDisplayController: or the override for searchDisplayController. Under iOS 4.3.2 initWithSearchBar:contentsController: appears to set searchDisplayController for the UIViewController instance passed as the contentsController argument. Perhaps this was a problem in earlier iOS releases, but it appears redundant in the current release.
Related
I am trying to add a title to my nav bar which I have added programmatically. I know i can create a label and add it as a subview, but then I need to add tons of configurations for it to be placed correctly like in a typical nav bar title. Is there any other way to do that than this:
- (void)viewDidLoad
{
[super viewDidLoad];
//Adding navBar programmatically
CGFloat width = self.view.frame.size.width;
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0,0,width,52)];
navBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
//Adding title programmatically
UILabel *lblTitle = [[UILabel alloc]initWithFrame:CGRectMake(10,10,80,30)];
lblTitle.text = #"Title";
[navBar addSubview: lblTitle];
[self.view addSubview:navBar];
}
And if not (sorry for another question, just think it relates) how do I set up my lblTitle to look like a normal nav bar title that you will find in most of the apps.
Thanks!
If you are adding the navigation bar by programmatically means, use this
(void)viewDidLoad {
[super viewDidLoad];
self.title = #"yourTitle";
}
You can also use
self.navigationItem.title = #"yourTitle";
You can use
navigationBar.topItem.title = #"Your Title";
The best answer to your question is just to use a UINavigationController to get its navigation bar for free, but if for some reason you can’t do that, you’ll need to use a custom UILabel and play with its properties until you match the style of the built-in one.
If a simple UILabel does not satisfy you, then yes, you need tons of configuration or you can do that in Interface Builder to polish your view. Instead of adding it as a subview, I suggest that you set it as navigationItem.titleView in your -(void)viewDidLoad.
How is a searchbar added above a UITableView? Would just the searchbar be enough or would the search bar and search display be chosen?
I want to redraw the tableview with the items that are found by the search.
UISearchDisplayController is specifically designed to solve your problem. You should be able to figure out how to use it from reading the documentation.
Of course you could just handle the UISearchBar (or even UITextField if you want to build your own search bar) yourself.
Here's some code to get you started:
- (void)viewDidLoad {
[super viewDidLoad];
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
searchBar.delegate = self;
self.tableView.tableHeaderView = searchBar;
searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
searchController.searchResultsDataSource = self;
searchController.searchResultsDelegate = self;
searchController.delegate = self;
}
Pretty much all there's left to do is to implement the delegates. If you need any help with that let me know, but may I suggest you ask a new question for each problem you encounter a long the way. Of course if you leave a comment here I will take a look at it.
I have a UIViewController which has a grouped UITableView as a property. I instantiate the UITableView in code and don't use IB. I would like to hook up a UISearchDisplayController to it but can't find any example how this could be done.
This what I have.
//Have implemented the UISearchDisplayDelegate in the header file
//SearchBar
UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 320, 45)];
searchBar.barStyle=UIBarStyleBlackTranslucent;
searchBar.showsCancelButton=NO;
searchBar.autocorrectionType=UITextAutocorrectionTypeNo;
searchBar.autocapitalizationType=UITextAutocapitalizationTypeNone;
searchBar.delegate=self;
UISearchDisplayController *mySearchDisplayController = [[UISearchDisplayController alloc ]initWithSearchBar:searchBar contentsController:self];
self.searchDisplayController = mySearchDisplayController; //Error here ?? self.searchDisplayController is ReadOnly and can't assign
[self.searchDisplayController setDelegate:self];
[self.searchDisplayController setSearchResultsDataSource:self];
[mySearchDisplayController release];
[myDisplayController release];
This doesn't seem to work, the searchDisplayController propery of the UIViewController seems to be readonly and I can't hook myDisplayController onto it. I'm really not sure if this the right way to do it.
I've been looking all around google to find some tip or tutorial on how to use a UISearchDisplayController in UIViewController. All the examples I could find was how to implement it into UITableViewController using IB, which is not the way I want to use it.
Can anyone explain how I could get this working ?
Here's the code that I use. Put this in viewDidLoad of a UIViewController that instantiates it's own UITableView. I think the part you're looking for is to add the search bar as the header view of the table view.
UISearchBar * theSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,0,320,40)]; // frame has no effect.
theSearchBar.delegate = self;
if ( !searchBarPlaceHolder ) {
searchBarPlaceHolder = #"What are you looking for?";
}
theSearchBar.placeholder = searchBarPlaceHolder;
theSearchBar.showsCancelButton = YES;
self.theTableView.tableHeaderView = theSearchBar;
UISearchDisplayController *searchCon = [[UISearchDisplayController alloc]
initWithSearchBar:theSearchBar
contentsController:self ];
self.searchController = searchCon;
[searchCon release];
searchController.delegate = self;
searchController.searchResultsDataSource = self;
searchController.searchResultsDelegate = self;
[searchController setActive:YES animated:YES];
[theSearchBar becomeFirstResponder];
See the Apple Docs:
#property(nonatomic, readonly, retain) UISearchDisplayController *searchDisplayController
Discussion: This property reflects the value of the
searchDisplayController outlet that you set in Interface Builder. If
you create your search display controller programmatically, this
property is set automatically by the search display controller when
it is initialized.
I have a UITableViewController that I have specified as a UISearchBarDelegate. Up until now, I had programmatically added the UISearchBar to the headerView of the table, and there were no problems.
I began to run out of screen real estate, so I decided to kill my normal UINavigationController title (which was text), and added the following code, moving my SearchBar from the table to the UINavigationBar:
// (Called in viewDidLoad)
// Programmatically make UISearchBar
UISearchBar *tmpSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,0,320,45)];
tmpSearchBar.delegate = self;
tmpSearchBar.showsCancelButton = YES;
tmpSearchBar.autocorrectionType = UITextAutocorrectionTypeNo;
tmpSearchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
[self set_searchBar:tmpSearchBar];
[tmpSearchBar release];
self.navigationItem.titleView = [self _searchBar];
This code works as expected - my UINavigationBar is now a UISearchBar.
However, my delegate method:
/** Only show the cancel button when the keyboard is displayed */
- (void) searchBarDidBeginEditing:(UISearchBar*) lclSearchBar
{
lclSearchBar.showsCancelButton = YES;
}
...is no longer being called. I've breakpointed, and I've confirmed that the UISearchBar's delegate is indeed self, the view controller. Oddly, this delegate method is still called just fine:
/** Run the search and resign the keyboard */
- (void) searchBarSearchButtonClicked:(UISearchBar *)lclSearchBar
{
_deepSearchRan = NO;
[self runSearchForString:[[self _searchBar] text] isSlowSearch:NO];
[lclSearchBar resignFirstResponder];
}
Any ideas why UINavigationBar is swallowing my delegate calls?? What am I missing?
I think you write the wrong method signature. It should be : – searchBarTextDidBeginEditing:
Here is all the UISearchBarDelegate methods for text editing.
– searchBar:textDidChange:
– searchBar:shouldChangeTextInRange:replacementText:
– searchBarShouldBeginEditing:
– searchBarTextDidBeginEditing:
– searchBarShouldEndEditing:
– searchBarTextDidEndEditing:
UISearchBarDelegate
In my view controller, when I instantiate my UISearchDisplayController object, I set its various properties, including the background color of the searchResultsTableView property:
self.searchDisplayController = [[[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self] autorelease];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
searchDisplayController.searchResultsDelegate = self;
UIView *tableViewHeaderBackground = [[UIView alloc] initWithFrame:CGRectMake(0,-480,480,480)];
tableViewHeaderBackground.backgroundColor = [UIAppDelegate defaultBackgroundTint];
[searchDisplayController.searchResultsTableView addSubview:tableViewHeaderBackground];
[tableViewHeaderBackground release];
I use this approach for coloring the space above the search bar when I am in the "normal" non-search table view, and this works properly there.
However, the tableViewHeaderBackground view does not appear to be a subview of this search display controller's table view. I do not see the defaultBackgroundTint that I applied to this subview.
Is there a correct way to add a background tint to the "outside" of the search results table view?