ios – 调用topLayoutGuide完全滚动?

前端之家收集整理的这篇文章主要介绍了ios – 调用topLayoutGuide完全滚动?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有topLayoutGuide方法的奇怪问题,我必须在setAutomaticallyAdjustsScrollViewInsets:不起作用的情况下使用.为了缩小问题的原因,我创建了以下最小的例子,它只是设置一个基本的表视图进行测试:

>在Xcode中设置新的iOS Single View应用程序.
>将以下代码粘贴到ViewController.m的实现中:

  1. @implementation ViewController
  2.  
  3. - (void)loadView
  4. {
  5. [self setTableView:[[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain]];
  6. }
  7.  
  8. - (void)viewDidLoad
  9. {
  10. [super viewDidLoad];
  11. [self setAutomaticallyAdjustsScrollViewInsets:NO]; // [*]
  12. }
  13.  
  14. - (void)viewDidLayoutSubviews
  15. {
  16. UITableView *tableView = [self tableView];
  17.  
  18. UIEdgeInsets insets = [tableView contentInset];
  19. // insets.top = [[self topLayoutGuide] length]; // [1]
  20. // insets.top = 100; // [2]
  21.  
  22. [tableView setContentInset:insets];
  23. [tableView setScrollIndicatorInsets:insets];
  24. }
  25.  
  26. #pragma mark - Table view data source
  27.  
  28. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  29. {
  30. return 100;
  31. }
  32.  
  33. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  34. {
  35. static NSString *CellIdentifier = @"Cell";
  36.  
  37. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  38.  
  39. if (cell == nil) {
  40. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  41. }
  42.  
  43. [[cell textLabel] setText:[NSString stringWithFormat:@"%d",[indexPath row]]];
  44.  
  45. return cell;
  46. }
  47.  
  48. @end

我遇到的问题是:

>如果我取消注释标有[1]的行,应用正确的插入,但滚动表视图不再有效.当我尝试滚动时,表格会弹出我的手指之后的初始滚动位置.这个行为也是通过对[self topLayoutGuide]的简单调用触发的,而不将结果分配给任何东西.
>如果我取消注释[2]而不是[1],则滚动工作. 100 pt的插图也被应用.但现在,初始滚动位置会自动调整,以使内容下滑状态栏.

([*]:这真的只是与含有导航控制器的任何东西相结合,在这个例子中我似乎并没有什么不同,但是我想禁用任何自动的行为,以确保.)

有什么明显的我做错了吗?我真的在这里亏了

解决方法

这绝对是iOS 7中的一个错误(似乎没有在7.1中修复),但似乎只影响UITableViewController.我切换到使用带有嵌入式UITableView的UIViewController,因为我不使用静态单元格,并且不需要UITableViewController为您提供的任何“魔术”.

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

这是一个可以接受的解决方法,直到苹果修复了这个bug.

猜你在找的iOS相关文章