3

我有一個UITableViewController與UISearchDisplayController安裝程序的標準方式(與tableView內的搜索欄)。我想要搜索欄開始隱藏 - 真的隱藏起來,不僅僅是滾動as in this solution。然後,我想在用戶按下按鈕時顯示搜索UI,並在用戶選擇搜索中找到的其中一個項目後再次隱藏(實際上隱藏它)。隱藏UITableView搜索欄

下面是該幾乎工作代碼:

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 

    self.searchDisplayController.searchBar.prompt = @"Add an item"; 
    self.searchDisplayController.searchBar.placeholder = @"Type the item name"; 

    // i do this to trigger the formatting logic below 
    [self.searchDisplayController setActive:YES animated:NO]; 
    [self.searchDisplayController setActive:NO animated:NO]; 
    // rest of my view will appear 
} 

- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller { 

    NSTimeInterval duration = (self.isViewLoaded && self.view.window)? 0.3 : 0.0; 
    __block CGFloat searchBarHeight = controller.searchBar.frame.size.height; 

    [UIView animateWithDuration:duration animations:^{ 
     self.tableView.contentOffset = CGPointMake(0, searchBarHeight); 
     self.tableView.contentInset = UIEdgeInsetsMake(-searchBarHeight, 0, 0, 0); // trouble here, I think 
    } completion:^(BOOL finished) { 
     controller.searchBar.hidden = YES; 
    }]; 
} 

顯示

- (IBAction)pressedAddButton:(id)sender { 

    self.searchDisplayController.searchBar.hidden = NO; 
    [self.searchDisplayController setActive:YES animated:YES]; 
} 

我想我正確地設置內容插圖,但表現異常,請:如圖所示的代碼,該表格允許內容向上移動得太遠,即只有兩個不是很高的行,我可以向下滾動,將這兩行中的大部分推到桌面上方......就像沒有底部反彈一樣。當我註釋掉contentInset行時,該表讓我將內容拉得太遠,在第0行之上留下了很大的差距,大概是搜索欄隱藏起來的地方。

非常感謝,如果任何人都可以提供幫助。

+0

隱藏搜索欄後,表格有2行,每個70px高度,表格高度= 367,內容offset.y = 0,內容inset.top = -75,inset.bottom = 0,全部爲預期。什麼讓我卡在這裏是什麼導致「底部反彈」?滾動視圖不應允許我滾動內容只有140px高,視圖367高,但爲什麼我可以滾動第一行? – danh 2012-03-21 14:53:03

回答

11

爲了別人誰可能有這個問題(這似乎是一個人) -

前進的唯一道路我可以找到的是放棄UITableViewController贊成UIViewController uiview的子級是表視圖和搜索欄。

我管理的佈局如上:

- (void)setSearchHidden:(BOOL)hidden animated:(BOOL)animated { 

    UISearchBar *searchBar = self.searchDisplayController.searchBar; 
    CGFloat searchBarHeight = searchBar.frame.size.height; 

    CGFloat offset = (hidden)? -searchBarHeight : searchBarHeight; 
    NSTimeInterval duration = (animated)? 0.3 : 0.0; 

    [UIView animateWithDuration:duration animations:^{ 
     searchBar.frame = CGRectOffset(searchBar.frame, 0.0, offset); 
     self.tableView.frame = UIEdgeInsetsInsetRect(self.tableView.frame, UIEdgeInsetsMake(offset, 0, 0, 0)); 
    } completion:^(BOOL finished) {if (!hidden) [searchBar becomeFirstResponder];}]; 
} 

除的方法被調用時附加功能開始和結束。 (大約每年兩次,我得出UITableViewController比它的價值更麻煩的結論,然後,以大致相同的頻率,我忘記了我學到了這一點)。如果有足夠的條目來完全填滿屏幕

[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO]; 

這僅適用於:

+0

這太棒了!我剛剛添加searchbar.alpha = 0;給動畫... – Hackmodford 2012-11-01 19:53:33

+1

+1爲「我忘了我學到了」:D – bennythemink 2014-02-10 03:07:55

0

與 「阿爾法」 的方法去:

[controller.searchBar setAlpha:0.0]; 

[controller.searchBar setAlpha:1.0]; 
+0

謝謝你的想法,但沒有區別。 – danh 2012-03-21 13:52:01

0

如果您的表有足夠的條目,你可以做以下。當然,如果你只有一個或兩個條目,你可能並不在乎隱藏搜索欄。