self.view
in a UITableViewController
is a UITableView
because the tableViewController overrides loadView
on the view controller.
If you run the following code:
UITableViewController *tableViewController = [[UITableViewController alloc] init];
NSLog(@"%@",tableViewController.view);
You would get the following console output:
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}>
As you can see, self.view
is not a UIView
but instead it is the UITableView
. Your separator view scrolls with the tableView because calling:
[self.view addSubview:separatorView];
is exactly the same as calling:
[self.tableView addSubview:separatorView];
If you wanted the separator to stay in a fixed position it would need to be added to the tableView/collectionView's superview and not the scrolling view itself.
The reason this is not happening in the UICollectionViewController
is because self.view
is actually a UICollectionViewControllerWrapperView
object. This is a private class but judging by its name, I am assuming it is a UIView
that wraps the UICollectionView
.
The most sensible way to achieve what you want would be to use a custom container view (child view controller) that would be a UIViewController
subclass containing your separator view and tableView/collectionView controller.
If you didn't want to add a child view controller then the other alternative would be to like you said, create a UIViewController
subclass and add your separator view and tableView as a subview within the view controllers view however if you don't use a UITableViewController
you lose bonus functionality like automatically adjusting the tableView's contentInsets for keyboard appearance etc.