调用topLayoutGuide完全打破滚动?

taj*_*hal 14 objective-c uiviewcontroller ios ios7

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

  1. 在Xcode中设置一个新的iOS Single View应用程序.
  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
    
    Run Code Online (Sandbox Code Playgroud)

我遇到的问题是:

  1. 如果我取消注释标记的行[1],则应用正确的插入,但滚动表视图不再有效.当我尝试滚动时,表格会在我松开手指后弹回到初始滚动位置.普通调用也会触发此行为[self topLayoutGuide],而不会将结果分配给任何内容.

  2. 如果我取消排队[2] ,而不是[1],滚动的作品.也应用了100 pt的插图.但现在自动调整初始滚动位置,以便内容重置状态栏.

([*]:这实际上似乎只与包含导航控制器结合使用.我在这个例子中似乎没什么区别,但我想禁用任何自动行为以确保.)

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

lid*_*ker 11

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

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

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