我正在尝试添加 UIView *separatorView
在我的 Collection View 和表格 View 的顶部,当相应 View 的 contentOffset.y > 0
出现时.通过在 viewDidLayoutSubviews
中添加 View ,我已经在我的收藏 View 上完成了这项工作。并将其添加到 self.view
.然后通过使用一些 ScrollView 委托(delegate)方法,我可以让它显示和隐藏。这很好用,但对于表格 View ,如果我使用相同的概念,separatorView 会随着表格 View 滚动。我仍在将 separatorView 添加到 self.view
但它的行为与 Collection View 不同。我不希望 separatorView 滚动。我希望它保持在顶部。我是否正确地认为使表格 View 工作的方法是子类 UIViewController
并与 separatorView 一起管理其中的表格 View ?有什么方法可以让它在我当前的UITableViewController
中工作吗?子类?
基本上,为什么 self.view
滚动表格 View 而不是 Collection View ?这似乎不一致。
谢谢您的帮助。
编辑:添加图片来展示想法。
最佳答案
self.view
在 UITableViewController
是 UITableView
因为 tableViewController 覆盖了 loadView
在 View Controller 上。
如果您运行以下代码:
UITableViewController *tableViewController = [[UITableViewController alloc] init];
NSLog(@"%@",tableViewController.view);
您将获得以下控制台输出:
2014-05-13 21:19:02.823 Test[28681:60b] <UITableView: 0x109834200; frame = (0 20; 320 548); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x109548f00>; layer = <CALayer: 0x109548c40>; contentOffset: {0, 0}>
如您所见,
self.view
不是 UIView
但它是 UITableView
.您的分隔 View 与 tableView 一起滚动,因为调用:[self.view addSubview:separatorView];
与调用完全相同:
[self.tableView addSubview:separatorView];
如果您希望分隔符保持在固定位置,则需要将其添加到 tableView/collectionView 的 super View ,而不是 ScrollView 本身。
UICollectionViewController
中没有发生这种情况的原因是因为 self.view
实际上是一个UICollectionViewControllerWrapperView
目的。这是一个私有(private)类(class),但从它的名字来看,我假设它是一个 UIView
包装UICollectionView
.实现您想要的最明智的方法是使用 custom container view (child view controller)那将是
UIViewController
包含分隔 View 和 tableView/collectionView Controller 的子类。如果您不想添加 subview Controller ,那么另一种选择是像您说的那样,创建一个
UIViewController
子类并将分隔 View 和 tableView 添加为 View Controller View 中的 subview ,但是如果您不使用 UITableViewController
你失去了额外的功能,比如自动调整 tableView 的 contentInsets 以适应键盘外观等。
关于ios - UITableViewController 和 UICollectionViewController 的 .view 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23640034/