2014-12-06 74 views
0

我想创建一个与NavigationBar上的searchBar的视图,我希望这个searchBar打开一个打开一个tableView与搜索结果,一旦一个项目被触摸时隐藏它。我对这个平台很陌生,所以我只需要一条路径,我不知道从哪里开始。UISearchBar与NavigationBar上的隐藏TableView

+1

为什么不给UISearchController一个镜头? – Andy 2014-12-06 01:40:29

+0

我试过了,但tableView关联停留在视图的其余部分之上,我想隐藏它直到搜索 – 2014-12-06 01:45:03

+1

这实际上很容易。虽然UISearchController是要走的路。周围的方法是添加一个搜索栏到你的tableviewheader并设置你的tableviews内容偏移你的搜索栏的高度。 – soulshined 2014-12-06 02:51:37

回答

1

根据我的评论:继承人更深入的解释。快乐编码:

.H

@interface TableViewController : UITableViewController <UISearchBarDelegate> 

@property (strong, nonatomic) UISearchBar *searchBar; 

@end 

.M

- (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 
[self.tableView setContentOffset:CGPointMake(0, 44)]; 
} 

而这就是这么多了。如果您想自定义其他的东西,如键盘布局和方式文本填充和颜色和字体和占位符文本等,可以在viewDidLoad中编辑或制作一个子类

编辑我已经包括了定制一些示例代码如下:

self.searchBar.keyboardAppearance = UIKeyboardAppearanceDark; 
self.searchBar.returnKeyType = UIReturnKeySearch; 
self.searchBar.searchBarStyle = UISearchBarStyleProminent; 
self.searchBar.barTintColor = [UIColor lightGrayColor]; 
self.searchBar.placeholder = @"Search for queries here"; 
self.searchBar.showsCancelButton = YES; 
[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: 
                           [UIColor blueColor], 
                           NSForegroundColorAttributeName, 
                           nil] 
                        forState:UIControlStateNormal]; 

-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar { 
self.searchBar.showsCancelButton = NO; 
[self.searchBar resignFirstResponder]; 
} 
1

您可以使用顶部的UISearchBar创建一个视图,然后创建一个UITableView(初始隐藏)并将其添加到您现有的视图。隐藏导航栏会获得与UISearchController相同的外观。然后,您可以在用户开始搜索时在搜索栏中显示表格视图委托。

相关问题