调用 topLayoutGuide 完全中断滚动? [英] Calling topLayoutGuide breaks scrolling altogether?
问题描述
我在使用 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:
- 在 Xcode 中设置一个新的 iOS Single View 应用程序.
在 ViewController.m 的实现中粘贴以下代码:
- Set up a new iOS Single View application in Xcode.
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]
的行,将应用正确的插图,但滚动表格视图不再有效.当我尝试滚动时,在我松开手指后,表格会弹回到初始滚动位置.此行为也由对[self topLayoutGuide]
的简单调用触发,无需将结果分配给任何内容.
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.
一旦我手动连接了 UITableViewDataSource
和 UITableViewDelegate
,我就可以开始使用 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 完全中断滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!