【发布时间】:2015-05-15 02:03:07
【问题描述】:
我正在将子视图添加到以编程方式添加的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++;
}
由于我需要在将 TableViews 添加到 ScrollView 时增加 scrollView 的内容大小,因此我创建了一个方法,每次添加表格时都会更新 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 = CGSize(x, y) -
我回滚了您的编辑。您不应该更改问题代码来“修复”您遇到的问题。这使得这个问题对未来的访问者毫无用处。
标签: ios objective-c uitableview uiscrollview subview