2014-12-06 71 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相同的外觀。然後,您可以在用戶開始搜索時在搜索欄中顯示錶格視圖委託。

相關問題