scrollsToTop不与UIViewController遏制工作

使用SDK 6.1,Xcode 4.6.1,我创build了一个新的项目Master-Detail iOS App,ARC,没有故事板。

然后在DetailViewController ,在viewDidLoad添加两个包含在UIViewControllerUITableView ,并确保第二个隐藏像这样:

 - (void)viewDidLoad { [super viewDidLoad]; UIViewController *lViewController1 = [[UIViewController alloc] init]; UITableView *lTableView1 = [[UITableView alloc] initWithFrame: self.view.frame]; lTableView1.scrollsToTop = YES; [lViewController1.view addSubview: lTableView1]; lTableView1.dataSource = self; [self.view addSubview: lViewController1.view]; [self addChildViewController: lViewController1]; UIViewController *lViewController2 = [[UIViewController alloc] init]; UITableView *lTableView2 = [[UITableView alloc] initWithFrame: self.view.frame]; lTableView2.scrollsToTop = YES; [lViewController2.view addSubview: lTableView2]; lTableView2.dataSource = self; [self.view addSubview: lViewController2.view]; [self addChildViewController: lViewController2]; // now hide the view in view controller 2 lViewController2.view.hidden = YES; } 

(我确定DetailViewController是一个返回100行UITableViewCell的数据源, textLabel.text设置为@"hello"

第二个视图控制器的存在使得scrollsToTop (点击状态栏上)不再起作用。 如果我不使用UIViewController遏制,只需添加两个UITableView并设置第二个隐藏, scrollsToTop不起作用。

我究竟做错了什么?

我正在试验你的项目。 什么时候

 lViewController2.view.hidden = YES; 

被replace

 lTableView2.hidden = YES; 

那么滚动工作,即使与控制器遏制。

我试图在控制器的视图和表之间插入一个视图,然后隐藏这个视图,但是表不滚动。

我试图隐藏控制器通过试验shouldAutomaticallyForwardAppearanceMethods但表不滚动。

结果:在我的实验中,只有一个滚动视图必须在视图层次结构中可见,并且不会检出父视图的hidden属性。 hidden所有其他滚动视图必须设置为NO ,而不是其父视图。

scrollsToTop只能在一个可见的视图上工作。 从文档 :

这个手势在单个可见的滚动视图上工作; 如果具有此属性集的多个滚动视图(例如dateselect器),或者委托在scrollViewShouldScrollToTop:返回NO ,则UIScrollView忽略该请求。 滚动视图滚动到内容视图的顶部之后,它将向委托发送scrollViewDidScrollToTop:消息。

您可以尝试[tableView setContentOffset:CGPointZero animated:YES]调用每个表(或滚动)视图上的[tableView setContentOffset:CGPointZero animated:YES] 。 为此,请在UIScrollViewDelegate协议中实现scrollViewShouldScrollToTop:方法:

 - (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView { [lTableView1 setContentOffset:CGPointZero animated:YES]; [lTableView2 setContentOffset:CGPointZero animated:YES]; return NO; } 

每个ViewController只能使用属性.scrollsToTop = YES来设置1个 ScrollView 。 如果你设置2 scrollview.scrollsTopTop = YES,它将停止运行。

 ie: your sample project (DetailViewController.m) update following lines, line48: lTableView1.scrollsToTop = YES; line56: lTableView2.scrollsToTop = NO; 

然后,scrollsToTop正常工作。 如果有超过1个滚动视图,你想同时setScrollsToTop,继续挖掘。 祝你好运!

经过testing几个选项和各种命中,并尝试我终于解决了一个最终的解决scheme,即setBounds:scrollView (这是tableView在你的情况),它的效果很好。 尽pipe如此,您仍然必须为animation付出额外的努力。

  CGRect frame = scrollView.frame; frame.origin.x = 0; frame.origin.y = 0; [scrollView setBounds:frame]; 

顺便你的情况,尝试返回YES

 - (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView; 

虽然如果没有定义,假设是。

我用这个,现在它工作正常。

 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. UIViewController *lViewController1 = [[UIViewController alloc] init]; UITableView *lTableView1 = [[UITableView alloc] initWithFrame: self.view.frame]; lTableView1.scrollsToTop = YES; [lViewController1.view addSubview: lTableView1]; lTableView1.dataSource = self; [self.view addSubview: lViewController1.view]; [self addChildViewController: lViewController1]; lTableView1.tag=1; UIViewController *lViewController2 = [[UIViewController alloc] init]; UITableView *lTableView2 = [[UITableView alloc] initWithFrame: self.view.frame]; lTableView2.scrollsToTop = NO; [lViewController2.view addSubview: lTableView2]; lTableView2.dataSource = self; [self.view addSubview: lViewController2.view]; [self addChildViewController: lViewController2]; lTableView2.tag=2; // now hide the view in view controller 2 lViewController2.view.hidden = YES; } - (NSUInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSUInteger)section { return 50; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString * const kCellIdentifier = @"MyCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellIdentifier]; } cell.textLabel.text = [NSString stringWithFormat:@"hello %d %d",indexPath.row, tableView.tag]; return cell; }