Hide UITableView search bar

╄→гoц情女王★ 提交于 2019-12-05 21:03:04

To anyone else who might have this problem (which appears to be nobody) -

The only way forward I could find was to abandon the UITableViewController in favor of a UIViewController with a uiview whose children are the table view and search bar.

I manage the layout as above:

- (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];}];
}

Except in a method that gets called when the add function starts and ends.

(About twice a year, I come to the conclusion that UITableViewController is more trouble than it's worth. Then, at approximately the same frequency, I forget I learned that).

Go with "alpha" approach :

[controller.searchBar setAlpha:0.0]; 

or

[controller.searchBar setAlpha:1.0]; 

If you have enough entries in your table, you can do the following:

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

This only works if there are enough entries to fully fill the screen. Of course, if you only have one or two entries, you may not care to hide the search bar.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!