将 UISearchBar 锁定到 UITableView 的顶部,例如 Game Center [英] Locking a UISearchBar to the top of a UITableView like Game Center

查看:28
本文介绍了将 UISearchBar 锁定到 UITableView 的顶部,例如 Game Center的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Game Center 的 UITableViews 和它们顶部的搜索栏有这个很酷的功能.与将搜索栏放置在表格标题视图中的应用程序不同(因此它被视为标准表格单元格),相反,它似乎被固定在其上方的父导航栏中.所以当滚动表格时,搜索栏确实会移动,但是如果你滚动到表格的边界之上,搜索栏将永远不会停止接触导航栏.

There's this cool feature in the UITableViews in Game Center and the search bars they have at their tops. Unlike apps where the search bar is placed in the table header view (so it counts as a standard table cell), instead, it seems to be bolted to the parent navigation bar above it. So when scrolling the table, the search bar does indeed move, but if you scroll above the boundaries of the table, the search bar never stops touching the navigation bar.

有人知道这是怎么做到的吗?我想知道 Apple 是否可能将搜索栏和表格都放在父滚动视图中,但我想知道它是否可能比这更简单.

Does anyone know how this might have been done? I was wondering if Apple maybe placed both the search bar and the table in a parent scroll view, but I'm wondering if it may be simpler than that.

推荐答案

Bob 的答案是相反的:它应该是 MIN(0, scrollView.contentOffset.y).

Bob's answer is reversed: it ought to be MIN(0, scrollView.contentOffset.y).

此外,为了正确支持调整大小(旋转时会发生),应重用其他帧值.

Also, in order to properly support resizing (which would occur when rotated), the other frame values should be reused.

-(void)scrollViewDidScroll:(UIScrollView *)scrollView 
{
    UISearchBar *searchBar = searchDisplayController.searchBar;
    CGRect rect = searchBar.frame;
    rect.origin.y = MIN(0, scrollView.contentOffset.y);
    searchBar.frame = rect;
}

这篇关于将 UISearchBar 锁定到 UITableView 的顶部,例如 Game Center的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆