給navigationItem中添加個搜索欄,效果和大部分程序一樣。代碼如下:
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 200, 28)];
searchBar.backgroundImage = [UIImage imageNamed:@"btu_search"];
searchBar.placeholder = @"搜索";
searchBar.delegate = self;
UIBarButtonItem *searchButton = [[UIBarButtonItem alloc]initWithCustomView:searchBar];
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObject:searchButton];
最後在- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar 方法裡面寫搜索方法就可以了。比較簡單,做個記錄。