scrollsToTop无法使用UIViewController包含

时间:2013-04-12 15:09:31

标签: iphone ios uiviewcontroller

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

然后在DetailViewController中,viewDidLoad我添加了UITableView中包含的两个UIViewController,并确保隐藏第二个- (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

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

第二个视图控制器的存在使得UIViewController(点击状态栏)不再起作用。如果我不使用UITableView包含,只需添加两个scrollsToTop并将第二个设置为隐藏,{{1}}就可以正常工作。

我做错了什么?

5 个答案:

答案 0 :(得分:8)

scrollsToTop仅适用于单个可见视图。来自documentation

  

此手势适用于单个可见滚动视图;如果有多个滚动视图(例如,日期选择器)设置了此属性,或者委托在NO中返回scrollViewShouldScrollToTop:UIScrollView将忽略该请求。滚动视图滚动到内容视图的顶部后,它会向代理发送scrollViewDidScrollToTop:消息。

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

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

答案 1 :(得分:4)

您只能为每个ViewController设置 1 ScrollView ,并且属性为.scrollsToTop = YES。 如果设置2 scrollview.scrollsTopTop = YES,它将停止运行。

ie: your sample project (DetailViewController.m) update following lines,

line48:    lTableView1.scrollsToTop = YES; 
line56:    lTableView2.scrollsToTop = NO;

然后,scrollsToTop正常工作。如果有多个滚动视图,你希望同时设置ScrollsToTop,继续挖掘。祝你好运!

答案 2 :(得分:3)

我目前正在尝试您的项目。当

lViewController2.view.hidden = YES;

替换为

lTableView2.hidden = YES;

然后滚动工作,即使控制器包含。

我尝试在控制器的视图和表之间插入一个视图,然后隐藏此视图,但表格没有滚动。

我试图通过试验shouldAutomaticallyForwardAppearanceMethods来隐藏控制器,但表格没有滚动。

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

答案 3 :(得分:0)

在测试了几个选项和各种匹配后尝试我最终确定了一个最终解决方案,即setBounds: scrollView(在您的情况下为tableView)并且它运行良好。你必须为动画付出额外的努力。

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

顺便提一句,请尝试将YES返回

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

虽然如果没有定义,则假定为。

答案 4 :(得分:0)

我已经使用了它,现在它工作正常。

- (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;
}