2014-02-27 52 views
2

背景:我有一個UIViewController,它具有以編程方式添加的UITableView。在UITableView的頂部,我有一個tablHeaderView,其中放置了一個UIView。這個UIView有一個UISearchBar和一個segmentedControl。這個想法是:用戶可以按照'日期/時間'或'位置'等基本類別對UITableView進行排序,也可以通過程序中的項目進行搜索。將UISearchBar添加到UITableView

問題:當我點擊搜索欄時,它調整大小(我不想),然後當我取消搜索時,它再次調整大小,直到UIViewController退出並重新加載。

Screenshot with first screen looking ok, but when you tap into the search bar it resizes and looks odd. You then cancel the search to get rid of it and it resizes again, taking up all of the tableHeaderView

代碼:

-(void)loadTableView 
{ 
    usableSpace = [[UIScreen mainScreen] applicationFrame]; 
    usableWidth = usableSpace.size.width; 
    usableHeight = usableSpace.size.height; 

    _tableView = [[UITableView alloc] init]; 
    [_tableView setFrame:CGRectMake(0,0,usableWidth, usableHeight)]; 
    [_tableView setDataSource:self]; 
    [_tableView setDelegate:self]; 
    [self.view addSubview:_tableView]; 

    _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, usableWidth, 44)]; 
    _searchBar.showsCancelButton = YES; 

    NSLog(@"searchBar height = %fl", _searchBar.frame.size.height); 
    segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Date/Time", @"Location", @"Speaker", nil]]; 
    segmentedControl.frame = CGRectMake(0, (_searchBar.frame.size.height), usableWidth, (usableHeight * 0.075)); 
    [segmentedControl addTarget:self action:@selector(sortList) forControlEvents:UIControlEventValueChanged]; 

    searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self]; 
    searchDisplayController.delegate = self; 
    searchDisplayController.searchResultsDataSource = self; 
    searchDisplayController.searchResultsDelegate = self; 

    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, usableWidth, (usableHeight * 0.15))]; 
    [headerView addSubview:_searchBar]; 
    [headerView addSubview:segmentedControl]; 
    _tableView.tableHeaderView = headerView; 

    [self.view addSubview:_tableView]; 
    self.tableView = _tableView; 

} 

我曾嘗試:我已經嘗試設置搜索欄的大小,而不是設置其大小。沒有它在tableHeaderView中的UIView中,而只是在它自己的。

爲什麼它調整大小,我該如何讓它停下來?

編輯:我剛纔試過如下:在故事板(其中的UIViewController最初創建)我在問題中選擇的UIVC並在屬性檢查器中我取消選擇「在最熱門酒吧」和「下底杆」和這似乎已經解決了動畫問題的第一部分。現在,當我點擊搜索欄時,搜索變得活躍,但searchBar不會調整大小。但是,當我取消搜索欄時,searchBar仍會按照截圖中的最後一張圖片進行動畫和調整大小。什麼可能導致調整大小?

+0

我有同樣的問題,我嘗試使用此解決方案。它工作正常。

回答

0

好吧,經過幾個小時的閱讀和嘗試不同的事情,我想我已經找到了如何有一個UISearchBar我想要它,它似乎工作正常。我相信問題是在tableHeaderView中有多個視圖,或者UISearchBar不喜歡在「包含」視圖中使用另一個視圖。

這是我結束的代碼,允許我在我的UITableView和UISearchBar的頂部有一個segmentedControl,它實際上位於我的UIView(添加了UITableView的頂部)的頂部。

-(void)loadTableView 
{ 
    usableSpace = [[UIScreen mainScreen] applicationFrame]; 
    usableWidth = usableSpace.size.width; 
    usableHeight = usableSpace.size.height; 

    _tableView = [[UITableView alloc] init]; 
    [_tableView setFrame:CGRectMake(0,0,usableWidth, usableHeight)]; 
    [_tableView setDataSource:self]; 
    [_tableView setDelegate:self]; 
    [self.view addSubview:_tableView]; 

    _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, usableWidth, 44)]; 
    [_searchBar setShowsCancelButton:YES]; 

    NSLog(@"searchBar height = %fl", _searchBar.frame.size.height); 
    segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Title", @"Date/Time", @"Speaker", nil]]; 
    segmentedControl.frame = CGRectMake(0, (_searchBar.frame.size.height), usableWidth, (usableHeight * 0.075)); 
    segmentedControl.selectedSegmentIndex = 0; 
    [segmentedControl addTarget:self action:@selector(sortList) forControlEvents:UIControlEventValueChanged]; 

    searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self]; 
    searchDisplayController.delegate = self; 
    searchDisplayController.searchResultsDataSource = self; 
    searchDisplayController.searchResultsDelegate = self; 

    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, usableWidth, (usableHeight * 0.15))]; 
    [headerView addSubview:segmentedControl]; 
    _tableView.tableHeaderView = headerView; 

    [self.view addSubview:_tableView]; 
    [self.view addSubview:_searchBar]; 
    self.tableView = _tableView; 
} 

這意味着我有: 的UIViewController與一個UINavigationController,的UISearchBar,UITableView的。 UITableView有一個segmentedConrol。 UISearchBar位於UINavigationController下方並始終存在。 UISearchBar位於UITableViewController的頂部,並與UITableViewCells一起滾動。

如果有人知道UISearchBar爲什麼會這樣做,那麼我將不勝感激,如果他們可以發表評論。我知道這個問題(或非常相似)已經被很多人所經歷過,我還沒有在任何地方找到明確的答案。

2

我一直在我的頭撞這太怪了很長...道具到this dude here for the clues

如果您希望您的UISearchBar與其他視圖一起位於標題中,並且使用標題滾動而不是粘在頂部,那麼您必須刪除它並在搜索完成後重新添加它。要清楚,像你已經做的那樣建立你的頭部,然後把它扔到那裏以處理棘手的動畫。

-(void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller { 
    [self.mySearchBar removeFromSuperview]; 
    self.mySearchBar.frame = CGRectMake(0, 0, 320, 44); 
    [self.table.tableHeaderView addSubview:self.mySearchBar]; 
} 

正如蒂莫西·穆斯指出here,「看來UISearchDisplayController使無證假設搜索欄是標題視圖的唯一內容」

...耶蘋果。