添加多个子视图时滚动视图不滚动

Scroll view not scrolling when more than one subview added

我正在向以编程方式添加的 UIScrollView 添加子视图,但是当子视图离开屏幕时,滚动视图不会滚动。有什么想法吗?

我需要多次更新滚动视图的内容大小。

首先我将滚动视图本身添加为视图的子视图,如下所示:

    [super viewDidLoad];

    [self addScrollView];
   [self APISimpleDemo];

}

#pragma mark Add Scroll View
-(void)addScrollView {

    //Add Scroll View
    CGFloat navHeight = self.navigationController.navigationBar.frame.size.height; //get height of nav bar
    CGRect scrollFrame;
    scrollFrame.size.height = self.view.frame.size.height - (navHeight - 150); //set height minus nav bar and top section
    scrollFrame.size.width = self.view.frame.size.width;
    scrollFrame.origin.y = 180;
    scrollFrame.origin.x = 0;

    self.scrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];
    NSLog(@"TableView Frame %@", NSStringFromCGRect(scrollFrame));
    self.scrollView.delegate = self;
    [self.view addSubview:self.scrollView];

}

NSLog 输出为:{{0, 180}, {375, 773}}

接下来代码运行,每个测试结束(在本例中为 2)一个 tableView 被添加到新添加的 scrollView 中。像这样...

-(void)addSpeedTable {


    CGRect frame = self.tableView.frame;
    frame.size.height = (40 * resultHeadings.count)+35;
    frame.size.width = self.view.frame.size.width;

    //If more than one table has been shown increase Y axis height for view
    if(tablesDisplayed > 0) {
        NSLog(@"Tables displayed greater than zero");
        viewHeight = viewHeight + frame.size.height;
    }

    frame.origin.y = viewHeight;
    frame.origin.x = 0;



    self.tableView = [[UITableView alloc] initWithFrame:frame];

   [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"newCell"];
    self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;

    self.tableView.rowHeight = 40;
    self.tableView.sectionFooterHeight = 0;
    self.tableView.sectionHeaderHeight = 22;
    self.tableView.scrollEnabled = NO; // Disable ScrollView


    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView = _tableView;
    NSLog(@"TableView Frame %@", NSStringFromCGRect(frame));

    [self.scrollView addSubview:self.tableView];
    tablesDisplayed++;
}

因为我需要在将 TableView 添加到 ScrollView 时增加 scrollView 的内容大小,所以我创建了一个方法,每次添加 table 时都会更新 scrollView 的内容大小:

代码现在看起来像:

-(void)addScrollView {

    //Add Scroll View
    CGFloat navHeight = self.navigationController.navigationBar.frame.size.height; //get height of nav bar
    CGRect scrollFrame;
    scrollFrame.size.height = self.view.frame.size.height - navHeight - 150; //set height minus nav bar and top section
    scrollFrame.size.width = self.view.frame.size.width;
    scrollFrame.origin.y = 180;
    scrollFrame.origin.x = 0;

    self.scrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];
    self.scrollView.delegate = self;

    [self.view addSubview:self.scrollView];

}

#pragma mark Update Scroll View Content Size
-(void)updateScrollSize {

//View height is set in the add table method depending on the size of the table
    CGFloat scrollHeight = 400 + viewHeight;

    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, scrollHeight);

}

-(void)addSpeedTable {


    CGRect frame = self.tableView.frame;
    frame.size.height = (40 * resultHeadings.count)+50;
    frame.size.width = self.view.frame.size.width;

    //If more than one table has been shown increase Y axis height for view
    if(tablesDisplayed > 0) {
        NSLog(@"Tables displayed greater than zero");
        viewHeight = viewHeight + frame.size.height;
    }

    frame.origin.y = viewHeight;
    frame.origin.x = 0;
    [self updateScrollSize];    //Update scrollView contentSize when adding new table to view


    self.tableView = [[UITableView alloc] initWithFrame:frame];

   [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"newCell"];
    self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;

    self.tableView.rowHeight = 40;
    self.tableView.sectionFooterHeight = 0;
    self.tableView.sectionHeaderHeight = 22;
    self.tableView.scrollEnabled = NO; // Disable ScrollView


    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView = _tableView;
    NSLog(@"TableView Frame %@", NSStringFromCGRect(frame));

    [self.scrollView addSubview:self.tableView];
    tablesDisplayed++;
}

您没有设置 scrollView.contentSize