Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 我不能';t在iPad上绘制完全渐变的背景_Ios_Iphone_Objective C_Ipad - Fatal编程技术网

Ios 我不能';t在iPad上绘制完全渐变的背景

Ios 我不能';t在iPad上绘制完全渐变的背景,ios,iphone,objective-c,ipad,Ios,Iphone,Objective C,Ipad,我用下面的代码在UICollectionView上绘制了渐变背景层 CAGradientLayer* collectionRadient = [CAGradientLayer layer]; collectionRadient.bounds = self.collectionView.bounds; collectionRadient.anchorPoint = CGPointZero; collectionRadient.colors = [NSArray arrayWithObjects:(

我用下面的代码在UICollectionView上绘制了渐变背景层

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];
它在iPhone模拟器上工作。但不是iPad。 下图是那个。背景层仅占用UICollectionView的一部分


我建议创建一个名为GradientView的UIView子类或类似的东西,用作背景视图

在GradientView的实现中,只需重载以下类方法:

+ (Class)layerClass 
{
    return [CAGradientLayer class];
}
然后,将上述代码更改为以下代码:

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];

我希望您的自定义GradientView的图层将自动调整大小。如果没有,您可能需要在自定义GradientView中实现layoutSubviews方法来调整渐变层的大小。

另一个注意事项:我在过去使用CAGradientLayer时遇到过一些带状问题。如果这不起作用,您可以考虑在CraveVIEW子类中实现DraceRT,以使用核心图形绘制渐变。谢谢您的响应。它工作得很好。还有一件事,我收到了一条警告消息,“不兼容的指针类型正在初始化'CAGradientLayer*\uu strong'和'CALayer*'类型的表达式”。是否应该明确地将其铸造到CAGRADENT?是的,您应该能够将层铸造到CAGRADENTLAYER。否则,您可以添加CAGradientLayer类型的只读属性,该属性只返回视图层的强制转换,这样该类的用户就不需要假定该层是渐变层。