私は本当に単純であるべきことをやろうとしています。ビューの 1 つにグラデーションを追加したいと考えています。self.view には追加できますが、viewWithGradient には追加できません。コードは次のとおりです。
CAGradientLayer *gradient = [CAGradientLayer layer];
UIColor *colorFirst = [UIColor colorWithWhite:0.10 alpha:0.15];
UIColor *colorLast = [UIColor colorWithWhite:0.535 alpha:0.8];
NSArray *colors = [NSArray arrayWithObjects:(id)colorFirst.CGColor, colorLast.CGColor, nil];
gradient.colors = colors;
NSNumber *stopFirst = [NSNumber numberWithFloat:0.00];
NSNumber *stopLast = [NSNumber numberWithFloat:1.00];
NSArray *locations = [NSArray arrayWithObjects:stopFirst, stopLast, nil];
gradient.locations = locations;
gradient.frame = CGRectMake(0.0, viewWithGradient.frame.origin.y, viewWithGradient.frame.size.width, height_of_gradient);
[self.view.layer addSublayer:gradient]; // this works. I get a nice gradient at
// the bottom of the view, where I want it,
// but I need the gradient further down in
// my view hierarchy.
// [viewWithGradient.layer addSublayer:gradient]; // this doesn't. There
// is no visually noticeable change
// if this is uncommented and the
// above line is.
viewWithGradient は、viewController (self.view) 内のメイン ビュー内の小さなビューです。この viewWithGradient に対する他のビューは 1 つだけです。これは、viewWithGradient の約半分の領域しか占めない UILabel です。背景は透明ですが、ラベルのテキストは白で描画されます。UILabelの上ではなく、self.viewではなく、UILabelの下にグラデーションを配置する必要があります。
私は現在、フレーム/境界がグラデーションを画面外に置く可能性があると考えています。誰かが私が見逃しているものを見ていますか? これは CALayer の使用法の中で最も単純なように思えますが、私はあまりにも多くの時間を費やしてきました。