调用topLayoutGuide会完全打破滚动吗

调用topLayoutGuide会完全打破滚动吗

本文介绍了调用topLayoutGuide会完全打破滚动吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am having weird issues with the topLayoutGuide method, which I have to use in a situation where setAutomaticallyAdjustsScrollViewInsets: doesn't work. To narrow down the cause of the problem, I've created the following minimal example, which just sets up a basic table view for testing:


  1. 在Xcode中设置一个新的iOS Single View应用程序。

  2. 在ViewController.m的实现中粘贴以下代码:

  1. Set up a new iOS Single View application in Xcode.
  2. Paste the following code in ViewController.m's implementation:

@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. 如果我取消注释标有<$的行c $ c> [1] ,应用了正确的插入,但滚动表视图不再有效。当我尝试滚动时,表格会在我松开手指后弹回到初始滚动位置。通过简单调用 [self topLayoutGuide] 也可以触发此行为,而无需将结果分配给任何内容。

  1. If I uncomment the line marked with [1], the correct inset is applied, but scrolling the table view no longer works. When I try to scroll, the table just bounces back to the initial scroll position after I release my finger. This behaviour is also triggered by a plain call to [self topLayoutGuide], without assigning the result to anything.

如果我取消注释行 [2] 而不是 [1] ,滚动作品。也应用了100 pt的插图。但现在初始滚动位置会自动调整,以便内容位于状态栏下方。

If I uncomment line [2] instead of [1], scrolling works. The inset of 100 pt is applied as well. But now the initial scrolling position is automatically adjusted so that the content underlaps the status bar.

[*] :这实际上似乎只能与包含导航控制器一起做任何事情。我似乎没有在这个例子中有所作为,但我想禁用任何自动行为可以肯定。)

([*]: This really only seems to do anything in combination with a containing navigation controller. I doesn't seem to make a difference in this example, but I want to disable any automatic behaviour just to be sure.)

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

Is there anything obvious I'm doing wrong? I'm really at a loss here.

推荐答案

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

This is definitely a bug in iOS 7 (doesn't appear to be fixed in 7.1) but seems to only affect UITableViewControllers. I switched over to using a UIViewController with an embedded UITableView since I am not using static cells and don't need any of the 'magic' that a UITableViewController gives you.

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

Once I wired up the UITableViewDataSource and UITableViewDelegate manually I was able to start using self.topLayoutGuide without messing up the tableView's contentSize.

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

This was an acceptable workaround for me until Apple fixes the bug.

这篇关于调用topLayoutGuide会完全打破滚动吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 17:50