首先,那些其他视图(表视图的同胞)是严格在表视图的上方和下方吗?如果是这样,你是否考虑让表视图正常滚动,并将这些外部视图放在表视图的页眉和页脚视图中?那么你不需要滚动视图。
其次,如果您尚未阅读,您可能需要阅读Technical Note TN2154: UIScrollView And Autolayout。第三,考虑到该技术说明中的信息,我可以想出几种方法来做你想做的事。最干净的可能是创建一个实现intrinsicContentSize
方法的UITableView
的子类。实现是微不足道的:
@implementation MyTableView
- (CGSize)intrinsicContentSize {
[self layoutIfNeeded]; // force my contentSize to be updated immediately
return CGSizeMake(UIViewNoIntrinsicMetric, self.contentSize.height);
}
@end
然后,让自动布局使用表视图的内在内容大小。在滚动视图的子视图(包括表格视图)之间创建约束以将其展开,并确保滚动视图的所有四个边都有约束。
你可能需要发送invalidateIntrinsicContentSize
以在适当的时间表格视图(当您添加或删除行或更改行的高度)。你可能只需重写MyTableView
中的适当方法即可。例如。做[self invalidateIntrinsicContentSize]
在-endUpdates
,-reloadData
,- insertRowsAtIndexPaths:withRowAnimation:
等
这里是我的测试结果:
滚动视图有淡蓝色背景。红顶标签和蓝底标签是滚动视图内的表视图的兄弟。
这是我测试中视图控制器的完整源代码。没有xib文件。
#import "ViewController.h"
#import "MyTableView.h"
@interface ViewController() <UITableViewDataSource, UITableViewDelegate>
@end
@implementation ViewController
- (void)loadView {
UIView *view = [[UIView alloc] init];
self.view = view;
UIScrollView *scrollView = [[UIScrollView alloc] init];
scrollView.translatesAutoresizingMaskIntoConstraints = NO;
scrollView.backgroundColor = [UIColor cyanColor];
[view addSubview:scrollView];
UILabel *topLabel = [[UILabel alloc] init];
topLabel.translatesAutoresizingMaskIntoConstraints = NO;
topLabel.text = @"Top Label";
topLabel.backgroundColor = [UIColor redColor];
[scrollView addSubview:topLabel];
UILabel *bottomLabel = [[UILabel alloc] init];
bottomLabel.translatesAutoresizingMaskIntoConstraints = NO;
bottomLabel.text = @"Bottom Label";
bottomLabel.backgroundColor = [UIColor blueColor];
[scrollView addSubview:bottomLabel];
UITableView *tableView = [[MyTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
tableView.translatesAutoresizingMaskIntoConstraints = NO;
tableView.dataSource = self;
tableView.delegate = self;
[scrollView addSubview:tableView];
UILabel *footer = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
footer.backgroundColor = [UIColor greenColor];
footer.text = @"Footer";
tableView.tableFooterView = footer;
NSDictionary *views = NSDictionaryOfVariableBindings(
scrollView, topLabel, bottomLabel, tableView);
[view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"V:|[scrollView]|"
options:0 metrics:nil views:views]];
[view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"H:|[scrollView]|"
options:0 metrics:nil views:views]];
[view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"V:|[topLabel][tableView][bottomLabel]|"
options:0 metrics:nil views:views]];
[view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"H:|[topLabel]|"
options:0 metrics:nil views:views]];
[view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"H:|-8-[tableView]-8-|"
options:0 metrics:nil views:views]];
[view addConstraint:[NSLayoutConstraint
constraintWithItem:tableView attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:view attribute:NSLayoutAttributeWidth
multiplier:1 constant:-16]];
[view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"H:|[bottomLabel]|"
options:0 metrics:nil views:views]];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
cell.textLabel.text = [NSString stringWithFormat:@"Row %d", indexPath.row];
return cell;
}
@end
您添加的UITableView在UIScrollView中????? – iPatel
是的,UITableView不占用整个可见区域。我很清楚UITableView有一个UIScrollView。要禁用滚动,我将tableView的高度设置为contentSize。 –
UITableView本身有一个滚动视图。那么为什么要添加到另一个scrollView? – Meera