gpt4 book ai didi

iphone - 隐藏 UITableView 搜索栏

转载 作者:可可西里 更新时间:2023-11-01 06:21:10 26 4
gpt4 key购买 nike

我有一个 UITableViewController 和一个以标准方式设置的 UISearchDisplayController(在 tableView 中有搜索栏)。我希望搜索栏一开始是隐藏的——真的是隐藏的,而不仅仅是滚开 as in this solution .然后我想在用户按下按钮时显示搜索 UI,并在用户选择在搜索中找到的项目之一后再次隐藏它(真正隐藏它)。

这是几乎可以工作的代码:

- (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 行上方留下一个很大的空隙,大概是搜索栏隐藏的地方。

如果有人能提供帮助,将不胜感激。

最佳答案

对于可能遇到此问题的任何其他人(似乎没有人)-

我能找到的唯一前进方向是放弃 UITableViewController,转而使用带有 uiview 的 UIViewController,其子级是 TableView 和搜索栏。

我按照上面的方式管理布局:

- (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 带来的麻烦多于它的值(value)。然后,以大致相同的频率,我忘记了我学到的东西)。

关于iphone - 隐藏 UITableView 搜索栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9798901/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com