14

我对这种方法有奇怪的问题,我必须在不起作用topLayoutGuide的情况下使用它。setAutomaticallyAdjustsScrollViewInsets:为了缩小问题的原因,我创建了以下最小示例,它只是设置了一个用于测试的基本表视图:

  1. 在 Xcode 中设置一个新的 iOS 单视图应用程序。
  2. 将以下代码粘贴到 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. 如果我取消注释标有 的行[1],则应用正确的插图,但滚动表格视图不再起作用。当我尝试滚动时,松开手指后表格会弹回初始滚动位置。此行为也由对 的普通调用触发[self topLayoutGuide],而无需将结果分配给任何东西。

  2. 如果我取消注释 line[2] 而不是,则[1]滚动有效。也应用了 100 pt 的插图。但是现在初始滚动位置会自动调整,以便内容与状态栏重叠。

[*]:这实际上似乎只与包含导航控制器的组合做任何事情。我似乎在这个例子中没有什么不同,但我想禁用任何自动行为以确保。)

有什么明显的我做错了吗?我在这里真的很茫然。

4

2 回答 2

11

这绝对是 iOS 7 中的一个错误(在 7.1 中似乎没有修复),但似乎只影响UITableViewControllers. 我转而使用UIViewController带有嵌入式的 a,UITableView因为我不使用静态单元格并且不需要 aUITableViewController给你的任何“魔法”。

一旦我手动连接UITableViewDataSource和连接,UITableViewDelegate我就可以开始使用self.topLayoutGuide而不会弄乱 tableView 的 contentSize。

在 Apple 修复错误之前,这对我来说是一个可以接受的解决方法。

于 2013-11-26T16:45:11.653 回答
2

对我来说同样的事情,而不是获得 topLayoutGuide,试试这个:

CGFloat topOffset = CGRectGetMaxY(self.refController.navigationController.navigationBar.frame);
于 2014-08-01T03:50:59.293 回答