我有topLayoutGuide方法的奇怪问题,我必须在setAutomaticallyAdjustsScrollViewInsets:不起作用的情况下使用.为了缩小问题的原因,我创建了以下最小的例子,它只是设置一个基本的表视图进行测试:
>在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 pt的插图也被应用.但现在,初始滚动位置会自动调整,以使内容下滑状态栏.
([*]:这真的只是与含有导航控制器的任何东西相结合,在这个例子中我似乎并没有什么不同,但是我想禁用任何自动的行为,以确保.)
有什么明显的我做错了吗?我真的在这里亏了