Pergunta

I am trying to add UIView *separatorView at the top of my collection views and table views which appears when the respective view's contentOffset.y > 0. I have made this work on my collection view by adding the view in viewDidLayoutSubviews and added it to self.view. Then by using some of the scroll view delegate methods I can make it show and hide. This works well but for the table view, if I use the same concept the separatorView scrolls with the table view. I am still adding the separatorView to self.view but it behaves differently than on the collection view. I don't want the separatorView to scroll. I want it to stay at the top. Am I correct in believing that the way to make this work for the table view is to subclass UIViewController and manage a table view within that along with the separatorView? Is there any way to make it work in my current UITableViewController subclass?

Basically, why does self.view scroll with the table view and not with the collection view? This seems inconsistent.

Thanks for the help.

Edit: Added picture to demonstrate idea.

enter image description here

Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top