首先,那些其他視圖(表視圖的同胞)是嚴格在表視圖的上方和下方嗎?如果是這樣,你是否考慮讓表視圖正常滾動,並將這些外部視圖放在表視圖的頁眉和頁腳視圖中?那麼你不需要滾動視圖。
其次,如果您尚未閱讀,您可能需要閱讀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