iphone - scrollsToTop 不适用于 UIViewController 包含

标签 iphone ios uiviewcontroller

使用 SDK 6.1、Xcode 4.6.1,我创建了一个新项目 Master-Detail iOS App,ARC,没有 Storyboard。

然后在 DetailViewController 中,在 viewDidLoad 中,我添加了两个包含在 UIViewController 中的 UITableView 并使确定第二个是这样隐藏的:

- (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 设置为 @"你好")

第二个 View Controller 的存在使得 scrollsToTop(点击状态栏)不再起作用。如果我不使用 UIViewController 包含,只是添加两个 UITableView 并将第二个设置为隐藏,scrollsToTop 确实有效。

我做错了什么?

最佳答案

scrollsToTop 仅适用于单个可见 View 。来自documentation :

This gesture works on a single visible scroll view; if there are multiple scroll views (for example, a date picker) with this property set, or if the delegate returns NO in scrollViewShouldScrollToTop:, UIScrollView ignores the request. After the scroll view scrolls to the top of the content view, it sends the delegate a scrollViewDidScrollToTop: message.

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

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

关于iphone - scrollsToTop 不适用于 UIViewController 包含,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15974893/

相关文章:

ios - 执行由 UITableViewCell 调用的 Segue

iphone - 写入 plist 字典中的字符串

ios - 在真实设备上调试 iAd 时变绿

java - Gmail API 服务器访问 "invalid_grant"iOS?

ios - NSInvalidArgumentException',原因 : '-[UITableView setSeparatorInset:]: unrecognized selector sent to instance

ios - 从目标 View Controller 推送新的 View Controller (快速)

iphone - 多种语言的多个 SQLite 数据库?

iphone - ViewController 的问题

ios - UIViewController 子类上的 Swift 可选或隐式属性

swift - 无法使用 segues 在 View Controller 之间传递数据(Xcode 7)(Swift)