Reputation: 193
I drew gradient background layer on UICollectionView with following code.
CAGradientLayer* collectionRadient = [CAGradientLayer layer];
collectionRadient.bounds = self.collectionView.bounds;
collectionRadient.anchorPoint = CGPointZero;
collectionRadient.colors = [NSArray arrayWithObjects:(id)[startColor CGColor],(id)[endColor CGColor], nil];
UIView* vv = [[UIView alloc] init];
self.collectionView.backgroundView = vv;
[self.collectionView.backgroundView.layer insertSublayer:collectionRadient atIndex:0];
It works on iPhone simulator. but not iPad. Below Image is that. the background layer is taking up only a part of UICollectionView.
Upvotes: 0
Views: 172
Reputation: 108
I would suggest creating a subclass of UIView named GradientView or something similar which you would use as your background view.
In your implementation of GradientView you just need to overload the following class method:
+ (Class)layerClass
{
return [CAGradientLayer class];
}
Then, change your code above to the following:
UIView* backgroundView = [[GradientView alloc] initWithFrame:self.collectionView.bounds];
backgroundView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
self.collectionView.backgroundView = backgroundView;
CAGradientLayer* collectionGradient = (CAGradientLayer *)backgroundView.layer;
collectionGradient.anchorPoint = CGPointZero;
collectionGradient.colors = [NSArray arrayWithObjects:(id)[startColor CGColor],(id) [endColor CGColor], nil];
I'm hoping that your custom GradientView's layer will automatically resize. If not, you may need to implement the layoutSubviews method in your custom GradientView to resize your gradient layer.
Upvotes: 1