【发布时间】:2013-10-25 16:55:23
【问题描述】:
我在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 的插图。但是现在会自动调整初始滚动位置,使内容与状态栏重叠。
([*]: 这似乎只与包含导航控制器结合使用。我在这个例子中似乎没有什么不同,但我想禁用任何自动行为以确保。)
有什么明显的我做错了吗?我真的很茫然。
【问题讨论】:
-
我刚刚发现对
[self topLayoutGuide]的调用导致表视图的contentSize属性从正确的值更改为{0, 0}。不过不知道为什么会这样…… -
可能是一个错误,也有那个。
-
这里也一样。这令人难以置信的沮丧。感觉就像旧的 iOS 3.0 时代一样好。
标签: ios objective-c uiviewcontroller ios7