我对这种方法有奇怪的问题,我必须在不起作用topLayoutGuide
的情况下使用它。setAutomaticallyAdjustsScrollViewInsets:
为了缩小问题的原因,我创建了以下最小示例,它只是设置了一个用于测试的基本表视图:
- 在 Xcode 中设置一个新的 iOS 单视图应用程序。
将以下代码粘贴到 ViewController.m 的实现中:
@implementation ViewController - (void)loadView { [self setTableView:[[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain]]; } - (void)viewDidLoad { [super viewDidLoad]; [self setAutomaticallyAdjustsScrollViewInsets:NO]; // [*] } - (void)viewDidLayoutSubviews { UITableView *tableView = [self tableView]; UIEdgeInsets insets = [tableView contentInset]; // insets.top = [[self topLayoutGuide] length]; // [1] // insets.top = 100; // [2] [tableView setContentInset:insets]; [tableView setScrollIndicatorInsets:insets]; } #pragma mark - Table view data source - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 100; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } [[cell textLabel] setText:[NSString stringWithFormat:@"%d", [indexPath row]]]; return cell; } @end
我遇到的问题是:
如果我取消注释标有 的行
[1]
,则应用正确的插图,但滚动表格视图不再起作用。当我尝试滚动时,松开手指后表格会弹回初始滚动位置。此行为也由对 的普通调用触发[self topLayoutGuide]
,而无需将结果分配给任何东西。如果我取消注释 line
[2]
而不是,则[1]
滚动有效。也应用了 100 pt 的插图。但是现在初始滚动位置会自动调整,以便内容与状态栏重叠。
([*]
:这实际上似乎只与包含导航控制器的组合做任何事情。我似乎在这个例子中没有什么不同,但我想禁用任何自动行为以确保。)
有什么明显的我做错了吗?我在这里真的很茫然。