调用topLayoutGuide打破了滚动?
我有一个奇怪的问题,我必须在setAutomaticallyAdjustsScrollViewInsets:
不起作用的情况下使用topLayoutGuide
方法。 为了缩小问题的原因,我创build了以下最简单的示例,它只是设置了一个基本的表视图来进行testing:
- 在Xcode中设置一个新的iOS Single View应用程序。
-
将以下代码粘贴到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]
的简单调用触发的,不需要将结果赋值给任何东西。 -
如果我取消注释行
[2]
而不是[1]
,滚动工作。 100点的插图也适用。 但是现在初始的滚动位置会自动调整,以便内容在状态栏下方。
( [*]
:这实际上只是和一个包含导航控制器的事情似的,我似乎在这个例子中没有什么区别,但是我想要禁用任何自动行为)。
有什么明显的我做错了吗? 我真的很茫然
这绝对是iOS 7中的一个错误(在7.1中似乎没有被修复),但似乎只影响UITableViewControllers
。 我切换到使用embeddedUITableView
的UIViewController
,因为我没有使用静态单元,并且不需要任何UITableViewController
给你的“魔术”。
一旦我连接UITableViewDataSource
和UITableViewDelegate
手动我能够开始使用self.topLayoutGuide
而不搞乱tableView的contentSize。
直到苹果修复这个错误之前,这对我来说是一个可以接受的解决方法。
同样的事情给我,而不是得到topLayoutGuide,试试这个:
CGFloat topOffset = CGRectGetMaxY(self.refController.navigationController.navigationBar.frame);