UITableVIew标头,下拉时不反弹 [英] UITableVIew header without bouncing when pull down
问题描述
我有一个UITableView
,我可以很容易地添加标题视图.许多应用程序(例如Facebook,用于查看事件)都具有headerView,当您下拉该标题时,标题视图保持不变,但表的其余部分(UITableViewCell
)都在弹跳.向上滚动时,标题消失.如何实现此功能?
I have a UITableView
which I am able to add a header view to fairly easily. Many apps (like Facebook, for viewing events) have a headerView that when you pull down, the header view stays put but the rest of the table (the UITableViewCell
's) are bouncing. When scrolling up the header disappears. How can I achieve this functionality?
现在当我按下UITableView
时,即使headerView也会反弹
Right now when I pull down the UITableView
, even the headerView bounces as well
推荐答案
通过向表头视图添加子视图并在表视图滚动到列表视图之外时调整其frame
或transform
,可以非常轻松地实现此效果.顶部,即其contentOffset
的y
分量变为负.
You can achieve this effect quite easily by adding a subview to the header view and adjusting its frame
or transform
when the table view is scrolled beyond the top, i.e. the y
component of its contentOffset
becomes negative.
示例(在UITableViewController
子类中):
- (void)viewDidLoad
{
[super viewDidLoad];
CGFloat headerHeight = 64.0f;
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, headerHeight)];
UIView *headerContentView = [[UIView alloc] initWithFrame:headerView.bounds];
headerContentView.backgroundColor = [UIColor greenColor];
headerContentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[headerView addSubview:headerContentView];
self.tableView.tableHeaderView = headerView;
}
//Note: UITableView is a subclass of UIScrollView, so we
// can use UIScrollViewDelegate methods.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat offsetY = scrollView.contentOffset.y;
UIView *headerContentView = self.tableView.tableHeaderView.subviews[0];
headerContentView.transform = CGAffineTransformMakeTranslation(0, MIN(offsetY, 0));
}
(为简单起见,我只是在scrollViewDidScroll:
中使用了实际标题视图的第一个子视图,您可能希望为此使用一个属性.)
(to keep it simple, I've just used the first subview of the actual header view in scrollViewDidScroll:
, you may want to use a property for that instead.)
这篇关于UITableVIew标头,下拉时不反弹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!