Reputation: 1088
I've two issues:
Code that I'm trying:
- (void)viewDidLoad {
[super viewDidLoad];
const NSInteger searchBarHeight = 45;
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0,
self.tableView.frame.size.width, searchBarHeight)];
searchBar.delegate = self;
self.tableView.tableHeaderView = searchBar;
[searchBar release];
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:@"Refresh"
style:UIBarButtonItemStyleBordered target:self action:@selector(onAddContact:)];
self.navigationItem.rightBarButtonItem = addButton;
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:20.0];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor whiteColor]; // change this color
self.navigationItem.titleView = label;
label.text = NSLocalizedString(@"All Contacts", @"");
[label sizeToFit];
content = [DataGenerator wordsFromLetters];
indices = [[content valueForKey:@"headerTitle"] retain];
}
Upvotes: 0
Views: 2136
Reputation: 20541
Here when you scroll the UITableView
at that time also UISearchBar
scrolled because you add UISearchBar
as a headerview of UITableView
see this line..
self.tableView.tableHeaderView = searchBar;
so its scroll with UITableView
, here for fix the UISearchBar
just add as a subview of self.view
with frame but outside from UITableView
For Example..
[yourSearchBar setFrame:CGRectMake(0, 0, 320, 44)];
[self.view addSubview:yourSearchBar];
For more information, See bellow links with tutorials and demo for UISearchBar
with UITableView
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
MethodUpvotes: 0
Reputation: 742
You need to separate the Search bar from Table View.Just add searchBar into view rather add it into table. The code is as follows:
[self.view addSubview:searchBar];
then add the table view.
For UISearchDisplayController example, please follow this tutorial: http://cocoabob.net/?p=67 or http://blog.mugunthkumar.com/coding/iphone-tutorial-uisearchdisplaycontroller-with-nspredicate/
Source code are also available. If you don't understand, please let me know. I have a project where all of these have already implemented.
Upvotes: 1