gpt4 book ai didi

iphone - 在导航 Controller 中默认隐藏 UITableView 搜索栏

转载 作者:可可西里 更新时间:2023-11-01 03:35:27 25 4
gpt4 key购买 nike

我已经阅读了多篇关于此的文章,但它对我来说工作不正常。我使用的是最新的 4.2 SDK。

我的代码是

self.tableView.contentOffset = CGPointMake(0.0, 44.0);

这部分有效,它将搜索栏向上移动了一点,但并没有完全隐藏。我试过将值 44 增加到更大的值,但这没有任何影响!我在表的 View Controller 的 viewDidLoad 方法中调用此代码。有人有什么想法吗?

最佳答案

另一种方法应该是...在 viewDidLoad 调用中:

self.tableView.contentInset = UIEdgeInsetsMake(-self.searchDisplayController.searchBar.frame.size.height, 0, 0, 0);

并实现 endDragging 委托(delegate)方法:

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
CGPoint offset = self.tableView.contentOffset;

CGFloat barHeight = self.searchDisplayController.searchBar.frame.size.height;
if (offset.y <= barHeight/2.0f) {
self.tableView.contentInset = UIEdgeInsetsZero;
} else {
self.tableView.contentInset = UIEdgeInsetsMake(-barHeight, 0, 0, 0);
}

self.tableView.contentOffset = offset;
}

设置内容是去掉一些“闪烁”

另外,如果您希望搜索栏位于顶部,请以这种方式实现 didScroll:

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGRect sbFrame = self.searchDisplayController.searchBar.frame;
sbFrame.origin.y = self.tableView.contentOffset.y;
if (sbFrame.origin.y > 0) {
sbFrame.origin.y = 0;
}
self.searchDisplayController.searchBar.frame = sbFrame;
}

我希望这会有所帮助(我花了几天时间才弄明白:))

干杯!

更新:

正如@carbonr 指出的那样,自 iOS7+ 起您应该在 viewDidLoad 中添加这一行

self.edgesForExtendedLayout = UIRectEdgeNone;

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

25 4 0