UIRefreshControl displays above UICollectionViews Cells

时间:2015-12-14 18:03:22

标签: c# ios xamarin uicollectionview

I use the following code to create the UIRefreshControl:

_refreshControl = new UIRefreshControl();
_refreshControl.ValueChanged += RefreshTriggered;
_collectionView.InsertSubview(_refreshControl, 0);

And I don't have any idea why the UIRefreshControl gets drawn over the UICollectionViewCells.

Here are the subviews of the UICollectionView when I just added the refresh control:

[0]: {<UIRefreshControl: 0x7c132fe0; frame = (0 0; 594 60); hidden = YES; autoresize = W; layer = <CALayer: 0x7c133250>>}
[1]: {<UIImageView: 0x79f8a5d0; frame = (3 588.5; 588 2.5); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = <CALayer: 0x79f89a70>> - (null)}
[2]: {<UIImageView: 0x79fab7d0; frame = (588.5 584; 2.5 7); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x796171f0>> - (null)}

And here are the subviews when the items are loaded in the UICollectionView:

[0]: {<MovieCollectionViewCell: 0x796e7350; baseClass = UICollectionViewCell; frame = (0 0; 183 278.16); clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x7c244bc0>>}
[1]: {<MovieCollectionViewCell: 0x7c2b5850; baseClass = UICollectionViewCell; frame = (186 0; 183 278.16); clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x7c2b5950>>}
[2]: {<MovieCollectionViewCell: 0x7c2ba7d0; baseClass = UICollectionViewCell; frame = (0 281; 183 278.16); clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x7c2ba8d0>>}
[3]: {<MovieCollectionViewCell: 0x7c2bf440; baseClass = UICollectionViewCell; frame = (186 281; 183 278.16); clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x7c2bf540>>}
[4]: {<MovieCollectionViewCell: 0x7c2c42d0; baseClass = UICollectionViewCell; frame = (0 562.5; 183 278.16); clipsToBounds = YES; hidden = YES; opaque = NO; layer = <CALayer: 0x7c2c43d0>>}
[5]: {<MovieCollectionViewCell: 0x7c2c9160; baseClass = UICollectionViewCell; frame = (186 562.5; 183 278.16); clipsToBounds = YES; hidden = YES; opaque = NO; layer = <CALayer: 0x7c2c9260>>}
[6]: {<UIRefreshControl: 0x7c132fe0; frame = (0 -69.5; 369 60); autoresize = W; layer = <CALayer: 0x7c133250>>}
[7]: {<UIImageView: 0x7c159e80; frame = (3 522; 363 2.5); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = <CALayer: 0x7c159fb0>> - (null)}
[8]: {<UIImageView: 0x7c15a480; frame = (363.5 -66.5; 2.5 224); opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x7c15a5b0>> - (null)}

Anyone an idea?

1 个答案:

答案 0 :(得分:6)

Try sending the refreshControll to the back after the layout process like this :

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];

    [self.collectionView sendSubviewToBack:self.refreshControl];
}

Because UITableView and UICollectionView reuse their cells, they have a very specific way of laying out their subviews (probably through a custom implementation of layoutSubviews). As a result, the position at which you insert a custom subview is usually not kept after the layout process. This is why you have to override viewDidLayoutSubviews and put your view back where you want it.

UIRefreshControll is automatically detected by UITableView and put back behind other subviews, but it's not the case for UICollectionView and you have to do it manually. And for any other custom subview, you would have to do it for both UITableView and UICollectionView.